carly.rb
), and run it a bunch of times, or you can just paste it into irb
and run just the last line a bunch of times.
me = ->{42} class Proc def maybe rand(2)==0 ? self : ->{} end end def call(it) it.call end puts call me.maybe
Adventures of a dinosaur evolving to meet today's challenges.
MOST OLD CONTENT HAS MOVED; see new home at
https://www.codosaur.us/blog/
carly.rb
), and run it a bunch of times, or you can just paste it into irb
and run just the last line a bunch of times.
me = ->{42} class Proc def maybe rand(2)==0 ? self : ->{} end end def call(it) it.call end puts call me.maybe
That probably makes most of you go WTF, and you probably figure the author meant "if status == 1 || status == 2 || status == 3" but somehow screwed up to the point of insanity, and rightly so. But bear with me, it's a fun little exploration of some bizarre Ruby usage.if status == status = 1 || status = 2 || status = 3
At line 001, we get a NameError because we haven't told it about x. At 002, x now exists (but is nil), because referencing it brought it to Ruby's attention. At 003, there is no NameError, so it can get to the assignment, but the old value was nil, so the comparison is effectively "nil == 1". At 004, we can see that it did indeed get assigned 1. At 005, the values finally match.$ irb :001 > x == x = 1 NameError: undefined local variable or method `x' for main:Object Did you mean? x from (irb):1 [...] :002 > x => nil :003 > x == x = 1 => false :004 > x => 1 :005 > x == x = 1 => true :006 >