How is a local variable created even when IF condition evaluates to false in Ruby?

Zack Xu

Try the following in irb: (I'm using Ruby 2.0.0-p247)

blah
#=> NameError: undefined local variable or method `blah' for main:Object

if false
  blah = 'blah'
end
#=> nil 

blah
#=> nil

I'm surprised that blah is assigned nil even when the if condition evaluates to false. I thought the code within if is skipped as the condition evaluates to false.

Could someone with Ruby internals knowledge kindly explain how this happened?

Thank you

Sergio Tulentsev

Local variables in ruby are created during parsing/compilation of code (not execution). They are lexically scoped, so a local variable is not visible before the line where it's assigned to.

defined?(foo) # => nil
if false
  defined?(foo) # => 
  foo = 'blah'
  defined?(foo) # => 
end

defined?(foo) # => "local-variable"
foo # => nil

defined?(foo) lines inside of if return nothing, because they didn't run. The assignment wasn't executed as well. However, the compiler saw the assignment to local variable and created one (with default value of nil).

This behaviour explains the trick from WAT talk:

a = a # => nil

Even though variable a doesn't exist, it is created (and set to nil) right before this line, simply because there is an assignment expression in the code (target of which is yet unknown local variable). So by the time the right hand side of this expression is evaluated, a exists.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What evaluates to false in Ruby?

From Dev

Variable always evaluates to false

From Dev

Variable always evaluates to false

From Dev

Do not understand why if condition evaluates to True when variable is integer

From Dev

Javascript form submits even when variable is false

From Dev

When local variable stack gets created?

From Dev

When local variable stack gets created?

From Dev

Statement inside If block executing even when condition is false

From Dev

Else Part is not executing even when condition is false Laravel 5.2

From Dev

<c:when test> evaluates always false

From Dev

ng-show element is visible even though the expression evaluates to false

From Dev

Python: if item in list evaluates False even though the item exists

From Dev

Check every condition in Python if else even if one evaluates to true

From Dev

Ruby if conditional weirdly appears to run some code when the condition is false

From Java

Fail a release pipeline job if custom condition evaluates to false

From Dev

Python if condition always evaluates to false regardless of user input

From Dev

Rails/Ruby how to pass 'all' as condition variable

From Dev

'And' condition evaluated even if previous condition is false in VBA

From Dev

'And' condition evaluated even if previous condition is false in VBA

From Dev

Internet Explorer 7+8 executes variable declaration inside IF even if condition is false

From Dev

Internet Explorer 7+8 executes variable declaration inside IF even if condition is false

From Dev

Keep child elements when data-sly-test evaluates to false?

From Dev

Angular 2 - Custom validator always returns true, even when condition is false

From Dev

Bash: 'if' statement always seems to evaluate first condition as true even when false

From Dev

javascript form validation always returns true even when condition matches and should returns false

From Dev

In C++ is it possible to write a lambda that is a condition i.e. just a condition that evaluates to true or false

From Dev

How to remove new of line in ruby variable created from system script

From Dev

Code execute even if where condition is false

From Dev

MSBuild builds target even though condition is false

Related Related

  1. 1

    What evaluates to false in Ruby?

  2. 2

    Variable always evaluates to false

  3. 3

    Variable always evaluates to false

  4. 4

    Do not understand why if condition evaluates to True when variable is integer

  5. 5

    Javascript form submits even when variable is false

  6. 6

    When local variable stack gets created?

  7. 7

    When local variable stack gets created?

  8. 8

    Statement inside If block executing even when condition is false

  9. 9

    Else Part is not executing even when condition is false Laravel 5.2

  10. 10

    <c:when test> evaluates always false

  11. 11

    ng-show element is visible even though the expression evaluates to false

  12. 12

    Python: if item in list evaluates False even though the item exists

  13. 13

    Check every condition in Python if else even if one evaluates to true

  14. 14

    Ruby if conditional weirdly appears to run some code when the condition is false

  15. 15

    Fail a release pipeline job if custom condition evaluates to false

  16. 16

    Python if condition always evaluates to false regardless of user input

  17. 17

    Rails/Ruby how to pass 'all' as condition variable

  18. 18

    'And' condition evaluated even if previous condition is false in VBA

  19. 19

    'And' condition evaluated even if previous condition is false in VBA

  20. 20

    Internet Explorer 7+8 executes variable declaration inside IF even if condition is false

  21. 21

    Internet Explorer 7+8 executes variable declaration inside IF even if condition is false

  22. 22

    Keep child elements when data-sly-test evaluates to false?

  23. 23

    Angular 2 - Custom validator always returns true, even when condition is false

  24. 24

    Bash: 'if' statement always seems to evaluate first condition as true even when false

  25. 25

    javascript form validation always returns true even when condition matches and should returns false

  26. 26

    In C++ is it possible to write a lambda that is a condition i.e. just a condition that evaluates to true or false

  27. 27

    How to remove new of line in ruby variable created from system script

  28. 28

    Code execute even if where condition is false

  29. 29

    MSBuild builds target even though condition is false

HotTag

Archive