Trans
7/18/2007 2:48:00 AM
On Jul 17, 6:52 pm, Jeff Pritchard <j...@jeffpritchard.com> wrote:
> I'm a relative newbie. I'm finally getting the hang of some of the
> syntactic sugar provided, such as the whole thing about using the "or"
> operator to provide a default value if something is nil:
>
> foo = bar || "emptiness"
>
> One thing I keep running into over and over and over and over that I
> wish there was some syntactic sugar for is the whole business of calling
> a method on an object, and doing something intelligent if the object is
> nil.
>
> If I have a string of stuff like:
> blah = foo.bar.split
>
> what if bar is nil? There are obvious long hand ways to deal with this,
> but then you loose the smoothness of lining up things like this in Ruby.
>
> I guess what I want is some syntactic sugar that means "this object, or
> an empty one of these if this is nil", so that I would get an empty
> result instead of a nil object missing method error.
>
> I would like to be able to write:
> blah = foo.bar||empty(bar).split
>
> This could be written:
> blah = foo.bar||"".split
blah = (foo.bar || "").split
Works just fine. Not sure what you mean by "well known object type".
T.