Ruby: method inexplicably overwritten and set to nil

后端 未结 2 813
醉梦人生
醉梦人生 2020-12-17 00:54

If I execute this ruby code:

def foo
  100
end

p defined?(foo), foo
if false
  foo = 200
end
p defined?(foo), foo

The output I get is:

2条回答
  •  一向
    一向 (楼主)
    2020-12-17 01:29

    This is what my pal and Ruby super-expert Josh Cheek had to say:

    When Ruby sees the assignment, it initializes the variable in the current scope and sets it to nil. Since the assignment didn't get run, it didn't update the value of foo.

    if statements don't change scope like blocks do. This is also the most important difference between

    for x in xs
    

    and

    xs.each { |x| }
    

    Here's another example:

    a = 123 if a  # => nil
    a  # => nil
    

    We shouldn't be able to say if a because we never set a, but Ruby sees the a = 123 and initializes a, then gets to if a at which point a is nil

    I'd consider it a quirk of the interpreter, really. Gary Bernhardt makes fun of it in wat (https://www.destroyallsoftware.com/talks/wat) with a = a

    -Josh

提交回复
热议问题