Why postfix `if` in Ruby works so strange

I have strange behavior in Ruby:

var1.zero? if var1 = 1

      

NameError

: undefined local variable or method var1

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?

+3


source to share


2 answers


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

+4


source


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?

.

0


source







All Articles