Why postfix `if` in Ruby works so strange
I have strange behavior in Ruby:
var1.zero? if var1 = 1
NameError
: undefined local variable or methodvar1
for main: Object
On the other hand, if I do the same in standard if
, everything works as expected:
if var1 = 1
var1.zero?
end
# => false
Can anyone describe how postfix works if
in Ruby?
source to share
Because if I ask you
Not my darling daughter ...
you interrupt me with
Do you have a daughter?
before I could finish my initial sentence, which was
Not my daughter is cute, is that her [display photo]?
But if I ask you
[showing the image] This is my daughter, isn't she cute?
you can answer easily
Not
source to share
It will go from left to right, first it will read var1.zero?
and thenif var1 = 1
var1.zero? if var1 = 1
why will he get var1
NameError: undefined local variable or method `var1 'for main: Object
And here,
if var1 = 1
var1.zero?
end
var1 = 1
will create var1 with value 1, so its error will not be thrown. when its execution var1.zero?
.
source to share