2007/7/18, Jeff Pritchard <jp / jeffpritchard.com>:
> Daniel Lucraft wrote:
> >> On Jul 17, 6:52 pm, Jeff Pritchard <j... / jeffpritchard.com> wrote:
> >>> This could be written:
> >>> blah = (foo.bar||"").split
> >
> > Sometimes I think the brackets spoil how the expression reads. You could
> > define a method that looks like this:
> >
> > blah = foo.bar.or("").split
> >
> > Trainwreck though...
> >
> > Dan
>
> Thanks everyone.  This has at least convinced me that I wasn't missing
> some well known way to do this.

I believe another (obvious?) solution has not been mentioned so far:

# note, I used the empty array as replacement
# because split would return an array
blah = foo.bar.split rescue []

I find it pretty elegant, too.

> I was looking for a solution which, like the || operator, works with all
> object types, not just a string object, so that it could be used as a
> general rather than a specific solution.
>
> In other words, the (something || "") solution works for split, because
> there is a convenient syntax for "empty string".
>
> I was hoping for a more general (something ||
> whateverneedstobeheretomakewhatfollowsworkright).fred
>
> As another poster mentioned or hinted at, since "something" may not have
> a type yet, there's no way to get the needed object type from that.

Well, you do not need the type.  You just need the method signature.
I.e., in your case something like this would be sufficient:

o=Object.new
def o.fred() your_default_value end

> I was hoping for something that can't be done without mind reading
> interpreters.

"can" or "can't"?

> Ruby rocks, but it still lacks a generalized "you know what I mean"
> operator.  :)

Hm...

Kind regards

robert