From: Rodrigo Rosenfeld Rosas Date: 2013-04-09T00:54:54+09:00 Subject: [ruby-core:54111] Re: [ruby-trunk - Feature #8237][Open] Logical method chaining via inferred receiver This is a multi-part message in MIME format. --------------020606080203010803070002 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Em 08-04-2013 11:50, Trans escreveu: > There are at least three other ways to approach this: > > user.try.profile.try.website.try.thumbnail > > user.trying{ |u| u.profile.website.thumbnail } > > user.trying.profile.website.thumbnail.resolve > > It occurs to me the first could be fairly concise if we think of #s > as possessive: > > user.s.profile.s.website.s.thumbnail > > Albeit it looks a bit odd. > I'd probably use _? instead of "s" for the andand approach: https://github.com/raganwald/andand class NullClass def method_missing(name, *args, &block); nil; end end module NullPattern def _?; nil? ? NullClass.new : self; end end BasicObject.send :include, NullPattern user._?.profile._?.website._?.thumbnail But anyway, this is kind of how it currently works using Groovy's pattern... What I don't understand is how you're thinking about implementing the CoffeeScript pattern: user.trying.profile.website.thumbnail.resolve By the way, I'd write it as: user._?.profile.website.thumbnail.resolve You can't inherit NullClass from NilClass. Also, you can't force an instance to behave like a falsey value in Ruby. Avdi made a great job explaining the reasons: http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/ So, how the "trying" approach would be implemented? Cheers, Rodrigo. --------------020606080203010803070002 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit Em 08-04-2013 11:50, Trans escreveu:
There are at least three other ways to approach this:

�� �� �� user.try.profile.try.website.try.thumbnail

�� �� �� user.trying{ |u| u.profile.website.thumbnail }

�� �� �� user.trying.profile.website.thumbnail.resolve

It occurs to me the first could be fairly concise if we think of #s as��possessive:

�� �� �� user.s.profile.s.website.s.thumbnail

Albeit it looks a bit odd.


I'd probably use _? instead of "s" for the andand approach:

https://github.com/raganwald/andand

class NullClass
�� def method_missing(name, *args, &block); nil; end
end

module NullPattern
�� def _?; nil? ? NullClass.new : self; end
end

BasicObject.send :include, NullPattern

user._?.profile._?.website._?.thumbnail

But anyway, this is kind of how it currently works using Groovy's pattern... What I don't understand is how you're thinking about implementing the CoffeeScript pattern:

user.trying.profile.website.thumbnail.resolve

By the way, I'd write it as:

user._?.profile.website.thumbnail.resolve

You can't inherit NullClass from NilClass. Also, you can't force an instance to behave like a falsey value in Ruby. Avdi made a great job explaining the reasons:

http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/

So, how the "trying" approach would be implemented?

Cheers,
Rodrigo.

--------------020606080203010803070002--