Excerpts from Stefan Rusterholz's message of Fri Jul 20 01:06:13 +0300 2007:
> Robert Dober wrote:
> On 7/19/07, Stefan Rusterholz <apeiros / gmx.net> wrote:
> >> so ._ simulates -> with that hack.
> 
> > I hate it, this really should be syntax and furthermore it would make
> > code ugly ( I know beauty lies in the eyes of the beholder ;).
> > The original idea is quite good, why your shift?
> > 
> > This is a terrible name pollution amongst other bad things :(
> > 
> > Robert
> 
> Keyword is "simulate" ;-)
> This can help trying it out. And I fully agree, that hack is ugly, also 
> I wouldn't use it in actual code simply because it is "magic" (you have 
> to know that _* methods are magic, that's not good for code to be 
> maintained).
> 
> Regards
> Stefan
> 
Hi, again
I've been thinking about this a bit more. Here's another approach you
could try. This actually feels a lot less like a hack.

Advantages:
- no namespace pollution
- clarifies what you're trying to do

Disadvantage:
- every time you call it you add an extra, intermediary call to, well,
  call() :)

However, I'm sure that I'm getting something wrong and, again, you can
simplify this to some extent, and improve it. It's just the idea itself
that I wanted to show you. I'm certain you'll find a way to ditch the 
call()

def nilsafe(&blk)
  x=Proc.new { Object.method_missing }
  def method_missing(meth,*args,&blk)
    return nil
  end

  begin
    yield blk
  rescue => e
    def method_missing(meth,*args,&blk)
      x.call(meth,args,&blk)
    end
    raise e
  end

  def method_missing(meth,*args,&blk)
    x.call(meth,args,&blk)
  end
end

So from an IRB prompt, after loading it, you can do:

irb(main):002:0> 3.foo.bar.bz
NoMethodError: undefined method `foo' for 3:Fixnum
        from (irb):2
irb(main):003:0> nilsafe { 3.foo.bar.baz }
=> nil
irb(main):004:0> nilsafe do
irb(main):005:1* 3.foo.bar.baz
irb(main):006:1> raise "Some Error here"
irb(main):007:1> end
RuntimeError: Some Error here
        from (irb):6
        from ./safeattr2.rb:8:in `nilsafe'
        from (irb):4
        from :0

the raise could be changed a little to output some nicer results (using caller) and other such niceties. But it is late now ... ;)

Let me know what you think of this one
Cheers,

-- 
Eugen Minciu.

Wasting valuable time since 1985.