Ruby local variable semantics
0. When ruby parser encounters assignment statement on some term looking like a local variable (a = 1, b ||= 2 etc.), it creates such variable. That’s why it knows later that “a” is a local variable, not a method call.
1. Sometimes this leads to a confusion:
def x; :x; end def y x = 42 if false # assignment is not executed x end y #=> nil (not :x as someone could predict)
2. But sometimes it helps a lot (merb):
# partial will have a local variable named "some_option" partial(template, object, :some_option => 1) # in this case there won't be a local variable "some_option" partial(template, object)Inside the partial you cannot simply write if some_option because it will raise NameError in case :some_option was not passed into the partial. However if (some_option ||= nil) works in either case.
Note: some_option || nil won’t work because it is not an assignment statement.
