ruby code
def something
p 'hellp something'
'hello'
end
p something + 'xx'
p '-----------'
something = something + 'xx'
The last code run error:
test01.rb:11:in
<main>': undefined method+' for nil:NilClass (NoMethodError)
This is my understanding of the last code:
- ruby explain the code from left to right.
- Ruby parser see 'something' first, then it will see '=' on the right. So, it think the 'something' as a variable and its value is nil.
- Ruby parser will see the second 'something', but do not know this is a variable or a method. So, it looks up the same name variable or method. Then it find the nil value 'something'. " nil + 'xx' " this code run with error.
I do not know whether my understanding is correct.