On Dec 27, 5:31 pm, Rodrigo Rosenfeld Rosas <[email protected]>
wrote:
> Changing the subject a bit, I've just remembered a feature of Groovy's
> language that I like and think that Ruby lacks...
>
> In Groovy:
>
> result = object?.method?.another
>
> would mean in Ruby something like:
>
> result = object && object.method && object.method.another
>
> I wonder if it would be possible to have a similar constructor in Ruby
> that was more DRY like in Groovy. Groovy's syntax would not be an option
> to Ruby since Ruby's methods accept method names ending with '?', which
> is not possible in Groovy. That is a good think but I can't think in an
> expressive annotation to be added to Ruby...

Rodrigo,

Funny you should mention that!  Hobo Support has the _? operator that
does just what you want.  (They mention in the documentation that they
wanted it to be called ?, but had to go with ._? to be valid Ruby.)
We use _? extensively in our application and frankly couldn't live
without it.  Here's a recent thread where I contributed a simplified
implementation of that method (inspired by ActiveRecord's
AssociationProxy BTW):

https://hobo.lighthouseapp.com/projects/8324/tickets/537-safenil-with-nil-public-methods-as-in-_to_i

--

You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.


Reply via email to