From: Rodrigo Rosenfeld Rosas <rr.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 <html> <head> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> </head> <body bgcolor="#FFFFFF" text="#000000"> Em 08-04-2013 11:50, Trans escreveu: <blockquote cite="mid:CAEu+kw20i-xOfxP_E0U1ANcFWujFER-yC1UOMwEgyjySrb47zA@mail.gmail.com" type="cite"> <div dir="ltr"> <div style=""><span style="font-family:arial,sans-serif;font-size:13px">There are at least three other ways to approach this:</span></div> <div style=""><span style="font-family:arial,sans-serif;font-size:13px"><br> </span></div> <div style=""><span style="font-family:arial,sans-serif;font-size:13px">�� �� �� user.try.profile.try.website.try.thumbnail</span><span style="font-family:arial,sans-serif;font-size:13px"><br> </span></div> <div style=""> <br> </div> <div><span style="font-family:arial,sans-serif;font-size:13px">�� �� �� user.trying{ |u| u.profile.website.thumbnail }</span><span style="font-family:arial,sans-serif;font-size:13px"><br> </span></div> <div><br> </div> <div><span style="font-family:arial,sans-serif;font-size:13px">�� �� �� user.trying.profile.website.thumbnail.resolve</span><span style="font-family:arial,sans-serif;font-size:13px"><br> </span></div> <div><span style="font-family:arial,sans-serif;font-size:13px"><br> </span></div> <div style=""><font face="arial, sans-serif">It occurs to me the first could be fairly concise if we think of #s as��possessive:</font></div> <div style=""><span style="font-family:arial,sans-serif;font-size:13px"><br> </span></div> <div> <div><span style="font-family:arial,sans-serif;font-size:13px">�� �� �� user.s.profile.s.website.s.thumbnail</span><span style="font-family:arial,sans-serif;font-size:13px"><br> </span></div> </div> <div><span style="font-family:arial,sans-serif;font-size:13px"><br> </span></div> <div style=""><span style="font-family:arial,sans-serif;font-size:13px">Albeit it looks a bit odd.</span></div> <div style=""><span style="font-family:arial,sans-serif;font-size:13px"><br> </span></div> </div> </blockquote> <br> I'd probably use _? instead of "s" for the andand approach:<br> <br> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <a href="https://github.com/raganwald/andand">https://github.com/raganwald/andand</a><br> <br> class NullClass<br> �� def method_missing(name, *args, &block); nil; end<br> end<br> <br> module NullPattern<br> �� def _?; nil? ? NullClass.new : self; end<br> end<br> <br> BasicObject.send :include, NullPattern<br> <br> user._?.profile._?.website._?.thumbnail<br> <br> 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:<br> <br> <span style="font-family:arial,sans-serif;font-size:13px">user.trying.profile.website.thumbnail.resolve</span><span style="font-family:arial,sans-serif;font-size:13px"><br> <br> By the way, I'd write it as:<br> <br> </span><span style="font-family:arial,sans-serif;font-size:13px">user._?.profile.website.thumbnail.resolve</span><span style="font-family:arial,sans-serif;font-size:13px"><br> </span><br> 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:<br> <br> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <a href="http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/">http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/</a><br> <br> So, how the "trying" approach would be implemented?<br> <br> Cheers,<br> Rodrigo.<br> <br> </body> </html> --------------020606080203010803070002--