I'm using ERB to generate a set of instructions with location data, and want to accept an offset in my input. When I try to check if that offset is present however, it sets it's value to nil. If anyone can help explain what's going on it'd be greatly appreciated!
Here's the code I ran to test this, once I'd narrowed down the problem:
<%
# apply a default value of 0 to the offset hash
p offset.nil?
p offset
p offset.nil?
p offset
if offset.nil?
p "I ran the code inside the if statement"
offset = {}
p "offset has been initialized, since it was nil"
end
p offset.nil?
p offset
%>
and the output:
false
{"z"=>-10}
false
{"z"=>-10}
true
nil
If I delete the if statement, I get the expected result of:
false
{"z"=>-10}
false
{"z"=>-10}
false
{"z"=>-10}
but having that if statement (or one like it) is kind of important. I've figured out a workaround with a begin-rescue block that basically does what the If was supposed to do, but I'm very curious why this was happening.
if offset.nil?and not something likeif offset = nilif offset.nil?exactly, no typo