R
Richard Drake
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
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