Two similar sentences have different behaviour. Is it ok?

Two similar sentences have different behaviour. Is it ok?

Compare:

1.9.3p374 :001 > a = 123 unless defined? a
=> nil
1.9.3p374 :002 > a
=> nil

but…

1.9.3p374 :003 > unless defined? b
1.9.3p374 :004?> b = 123
1.9.3p374 :005?> end
=> 123
1.9.3p374 :006 > b
=> 123

its because is the first sample a= is parsed first so it knows that its
an local variable, thats why defined? a returns true

using defined is not an good idea, showing this sample:

if false
a = 2
end
a #=> nil

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs