4 ms·
Didn't see anyone mention this little gem about Ruby: a = [1,2,3] x = :foo puts x a = a.map do |x| x+1 end See anything wrong here? (hint: what is the
by jluxenberg 16y ago
Didn't see anyone mention this little gem about Ruby:
a = [1,2,3]
x = :foo
puts x
a = a.map do |x|
x+1
end
See anything wrong here? (hint: what is the value of x?)
- prog 16y agoYes, thats surprising behavior. Fortunately its fixed in 1.9. :) Spoiler alert: . . . 1.8.x produces 3 while 1.9 produces :foo
- jluxenberg 16y agoHmm, interesting. I suspect this will break many things, but I'm happy that they have changed it.
- kemiller 16y agoThe problem is not as pervasive as you might think, though it does happen.
- lincolnq 16y agoWhy was this broken in the first place? (What abomination of semantics would cause this behaviour)?
- gnaritas 16y agoClosure variables not having their own scope and shadowing method level vars. They're simply implemented as method level vars, so not true closures.
- bartl 16y agoAll I can see is that this code makes no sense whatsoever to me. I guess that means that Ruby is not for me...
- Zak 16y agoAny programming language you don't know is bound to look something like line noise with some words mixed in.
- JadeNB 16y ago> Any programming language you don't know is bound to look something like line noise with some words mixed in. Nuh-uh, not APL! That looks like line noise with no words mixed in. :-)
- gcv 16y agoHere's another fun thing in Ruby: irb(main):001:0> x = true or "hi" => true irb(main):002:0> x => true irb(main):003:0> x = false or "hi" => "hi" irb(main):004:0> x => false What? Short-circuit evaluation doesn't work in an assignment..? Oh. irb(main):005:0> x = (false or "hi") => "hi" irb(main):006:0> x => "hi" In fairness, the || operator does not require explicit parentheses — but I still hold that operator precedence is just evil.
- brettnak 16y agoI actually was bitten by this just 2 weeks ago. It took me quite a while to figure this out.
- jpr 16y agoSometimes I wonder whether I should really try Ruby, but brainfarts like these always remind me why I decided against it the last time.