5

I wonder, why is a visible?

if true
  puts 'true'
else
  puts 'false'
  a = 123
end

puts a # no error

# or 
# my_hash = {key: a}
# puts my_hash # :key => nil

But this causes an error, even though there will be 'true' shown

if true
  puts 'true'
else
  puts 'false'
  a = 123
end

puts a2 # boooooom
1

1 Answer 1

1

Referencing a inside the if has the effect of declaring it as a variable if there is no method a= defined for the object.

Since Ruby does not require methods to be called using the same syntax as referencing a variable or assigning to one, it needs to make an assessment as to the nature of the token in question. If it could be a method call because a method with that name has been defined, then it will be interpreted as such. If no such method exists at the time the source is compiled, then it will be a variable by default.

Sign up to request clarification or add additional context in comments.

4 Comments

"value should be scoped to the if block"? I'm not sure what that means? Variables are scoped, and a is not scoped to the if.
if has no block, only clauses that are conditionally evaluated. The clauses of an if statement do not create a new scope. This is not C.
I've always treated the if as if it was scoped, but you're right, that doesn't appear to be the case. C++ habits refuse to go away. Variables will be scoped to the method they're declared inside of, or the main context if in irb.
Whether or not there is a method a= defined is completely irrelevant. a = foo will always assign to a local variable called a. In fact, that is how Ruby distinguishes between variables and methods in the first place! If you want to call a setter called a=, you have to use self.a = foo. That's why setters are exempt from the privacy rule that private methods can only be called without an explicit receiver, because there wouldn't be a way to call a private setter otherwise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.