From: Justin Collins Date: 2006-01-25T05:52:35+09:00 Subject: Re: readable and provocative & daring Some interesting stuff there...but I think the main thing is that 'and' is a Ruby keyword, whereas '&' is an instance method. '&' can be defined as makes sense for whatever object it is being called on. Therefore, something like >readable and provocative & daring is equivalent to >readable and provocative.&(daring) which means if readable is false, then provocative.& doesn't need to be called at all. However, if readable is true, then it does something like >provocative.send(:&, daring) If provocative doesn't exist, it can't call any methods on it. If provocative does exist, but daring doesn't, then daring cannot be evaluated and can't be passed in as a parameter to the function call. However, once provocative and daring are defined, it can try to call the provocative.& method, if it exists. And since you define it, all is good! My apologies if you already realized all that! The way you have the syntax and the fact that "and" and "&" are equivalent in English could be confusing... -Justin Richard Drake wrote: > A funny thing happened to me in irb this week. Well, to be honest it's > a reconstruction. And it was little more than one thing. But hopefully > you'll get the gist. > > irb(main):001:0> readable = false > => false > irb(main):002:0> readable and provocative & daring > => false > irb(main):003:0> readable = nil > => nil > irb(main):004:0> readable and provocative & daring > => nil > irb(main):005:0> readable = "yes" > => "yes" > irb(main):006:0> readable and provocative & daring > NameError: undefined local variable or method `provocative' for > main:Object > from (irb):6 > irb(main):007:0> provocative = false > => false > irb(main):008:0> readable and provocative & daring > NameError: undefined local variable or method `daring' for main:Object > from (irb):8 > > Fine so far. & is eager, it always seeks to evaluate its right hand > side. Fair cop. Let's move on. > > irb(main):009:0> daring = nil > => nil > irb(main):010:0> readable and provocative & daring > => false > irb(main):011:0> provocative = nil > => nil > irb(main):012:0> readable and provocative & daring > => false > > Hmm, a slightly surprising difference with and there. But does it > matter? It didn't to me at the time. I was Ruby newbie with an > important business problem to solve ... > > irb(main):013:0> provocative = "yes " > => "yes " > irb(main):014:0> readable and provocative & daring > NoMethodError: undefined method `&' for "yes ":String > from (irb):14 > > Aha. So I defined a little method in String and then ... > > irb(main):020:0> readable and provocative & daring > => nil > irb(main):021:0> daring = "please" > => "please" > irb(main):022:0> readable and provocative & daring > => "yes please" > > Yep, that's it. Now let's really take this thing out for a spin: > > irb(main):023:0> daring = [1000, " times ", "yes"] > => [1000, " times ", "yes"] > irb(main):024:0> readable and provocative & daring > => "yes 1000 times yes" > irb(main):025:0> provocative = ["please", "say", "yes"] > => ["please", "say", "yes"] > irb(main):026:0> readable and provocative & daring > => ["yes"] > > Hmm. > > The elided code was of course > > class String > def & a > self + a.to_s if a > end > end > > This was I stress again to solve a real world problem. Or, to be more > precise, to make an existing solution to a real world problem more > readable. > > But it did feel a bit provocative and daring. > > Any comments? > > Richard > > ---- > > Ruby for laser purity > > >