I've been playing around with Ruby and have come across this weird behavior. Could anyone please tell me why this is happening?
if I run:
def my_fn
if false then
a = {:a=>10, :b=>20}
end
puts a.class
a || {}
end
and print the result the code compiles successfully and returns {}. But if I change the code to:
def my_fn
puts a.class
a || {}
end
it doesn't return {} but throws an Error. "'my_fn': undefined local variable or method 'a' for main:Object (NameError)"
Shouldn't a just be nil and not cause an error?