# Some thoughts on Object#try

**URL:** <https://discuss.rubyonrails.org/t/some-thoughts-on-object-try/41966>\
**Category:** rubyonrails-core\
**Created:** [April 27, 2009, 3:02am UTC](https://discuss.rubyonrails.org/t/some-thoughts-on-object-try/41966 "2009-04-27T03:02:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mateo\_Murphy](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@Mateo\_Murphy](https://discuss.rubyonrails.org/u/Mateo_Murphy)\
**Post date:** [April 27, 2009, 3:02am UTC](https://discuss.rubyonrails.org/t/some-thoughts-on-object-try/41966/1 "2009-04-27T03:02:55Z")

</div>

Apologies in advance if this has been covered here before, but it's hard to do a search on a term like "try"

First off, I've been thinking that the current behavior of try may not be the most logical:

Compare

> > nil.to\_s

=\> ""

vs.

> > nil.try(:to\_s)

=\> nil

Would it not make more sense for NilClass to return nil on try only for those methods it doesn't respond to itself? For the vast majority of cases it won't make a difference, but it just seems more logical to me that way.

Secondly, I'd like to suggest an alternate syntax for try, using a proxy object:

i.e.

> > object.try.method\_name

rather than

> > object.try(:method\_name)

I've been using this myself for a while, and I find it more natural than the send style syntax, which I also support for those cases where it's needed.

Incidently, I also set up a not proxy, so that I can do

> > something.not.nil?

rather than

> > !something.nil?

Which I find reads better as well.

In any case, I can submit a patch with an of these changes, if there's any interest?

Cheers,

mateo

---

<div class="post-metadata">

**Author:** ![Mike\_Gunderloy](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@Mike\_Gunderloy](https://discuss.rubyonrails.org/u/Mike_Gunderloy)\
**Post date:** [April 27, 2009, 3:10pm UTC](https://discuss.rubyonrails.org/t/some-thoughts-on-object-try/41966/2 "2009-04-27T15:10:46Z")

</div>

You may want to take a look at some of the past discussion:

[http://github.com/rails/rails/commit/51730792ca930a896361eb92354a42bc56903de1](http://github.com/rails/rails/commit/51730792ca930a896361eb92354a42bc56903de1)[https://rails.lighthouseapp.com/projects/8994/tickets/1425](https://rails.lighthouseapp.com/projects/8994/tickets/1425)

before reopening this particular can of worms.

Mike

---

<div class="post-metadata">

**Author:** ![Mateo\_Murphy](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@Mateo\_Murphy](https://discuss.rubyonrails.org/u/Mateo_Murphy)\
**Post date:** [April 28, 2009, 12:38am UTC](https://discuss.rubyonrails.org/t/some-thoughts-on-object-try/41966/3 "2009-04-28T00:38:17Z")

</div>

Thanks, I was aware of the discussion around the final implementation, but had missed what came earlier. I didn't see anything regarding the points I brought up, although I may have missed that as well.

In any case, I'm don't care much about NilClass#try returning nil as it's never caused a problem for me, but I am quite partial to the proxy syntax; is there a drawback to supporting that that I've missed?

mateo

---

<div class="post-metadata">

**Author:** ![MatthewRudy](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@MatthewRudy](https://discuss.rubyonrails.org/u/MatthewRudy)\
**Post date:** [April 28, 2009, 9:22am UTC](https://discuss.rubyonrails.org/t/some-thoughts-on-object-try/41966/4 "2009-04-28T09:22:31Z")

</div>

> \>\> object.try.method\_name

Check out andand.

that's the same as this.

something.andand.somemethod
