operator precedence - Why does the block of this if statement seem to be executed before the conditional in ruby? -
this question has answer here:
i'm working on a codegolf challenge, , i'm trying use few characters possible.
this code:
#example data x=[1,2,3] a=5.times.map{5.times.map{' '}} #problematic code: a[b][c]=x.pop if a[b=rand(5)][c=rand(5)] == ' '
returns (quite strange) error:
test.rb:5:in `<main>': undefined local variable or method `b' main:object (nameerror) did mean? b
which boils down "can't find b
, did mean b
?". (to answer yes).
this happens 2d array inline if statement.
this code runs fine:
if a[b=rand(5)][c=rand(5)] == ' ' a[b][c]=x.pop end
it seems should same, behaves differently. why this?
i thought might have precedence, had @ the ruby precedence table , seem expect, i'm not sure.
Comments
Post a Comment