Interpretation as a local variable overrides method name?

后端 未结 3 1783
北荒
北荒 2020-12-11 07:23

As in this question, when a local variable not defined is used within its own assignment, it is evaluated to nil.

x = x # => nil 
         


        
3条回答
  •  攒了一身酷
    2020-12-11 07:29

    Short answer is, because Matz defined it so. This behavior is one of the very few things I don't like about Ruby. It even gets better:

    a = b if a
    => nil
    a
    => nil
    

    Variable a gets initialized to nil even though in theory a = b statement should under no circumstances be executed.

提交回复
热议问题