From: Yusuke ENDOH Date: 2009-02-07T22:47:46+09:00 Subject: [ruby-core:21907] Re: [Feature #1122] request for: Object#try Hi, 2009/2/7 Narihiro Nakamura : > Feature #1122: request for: Object#try > http://redmine.ruby-lang.org/issues/show/1122 > > Author: Narihiro Nakamura > Status: Open, Priority: Normal > > Hi. > > Object#try is new feature of rails2.3. > > http://guides.rubyonrails.org/2_3_release_notes.html > http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/try.rb > http://ozmm.org/posts/try.html > > Matz said: "OK, good name is the last problem." > > Anyone has better name? Interesting. I think `try' is good enough for me as non-native. But why does it take Object#send-like interface? I dislike Object#send because complex modifications are needed: foo.bar(baz) -> foo.send(:bar, baz) I have to type "send(:", move the cursor, delete "(", and type ", ". What a pain! Of course, it can't be helped in the case of Object#send. But there is no reason for Object#try to follow it. I prefer: foo.bar(baz) -> foo.try.bar(baz) because it is much simpler (only to insert ".try") For example: x = nil; p x.try + 1 #=> nil x = 100; p x.try + 1 #=> 101 Implementation image: class Object def try self end end class NilClass def try obj = BasicObject.new def obj.method_missing(*) nil end obj end end -- Yusuke ENDOH