language-ruby
language-ruby copied to clipboard
Local variables?
Why every type of variables except local ones supported, is this intended or ...?
Ruby's grammar is ambiguous when it comes to the left-hand side of assignment expressions. In this very limited case, this is a local variable:
def foo
bar = 5
end
But if we expand it to this:
class Foo
def bar=(value)
@bar = value
end
def foo
bar = 5
end
end
Then bar
is a method, not a local variable. Without having a full parse tree, it is impossible in Ruby to know if an assignment statement is an assignment to a local variable or a function call. (And even with the full parse tree, it can still be ambiguous since methods can be generated at runtime.)
Actually, in both cases, bar
is a local variable.
Ruby always treats assignment as assignment rather than a method call (this is where self.bar =
comes into play, as this ensures we're calling the method.)
Thanks for the correction. My example was bad.
Well I hope you aren't just calling and breathing into the mic instead of sending a message :heart_eyes_cat: :wink: