Ruby hash value truthiness and symbols

Posted by John Topley on Stack Overflow See other posts from Stack Overflow or by John Topley
Published on 2010-05-09T10:56:45Z Indexed on 2010/05/09 10:58 UTC
Read the original article Hit count: 164

Filed under:

Could somebody please explain why the variable named foo remains true in the code below, even though it's set to false when the method is called? And why the symbol version behaves as expected?

def test(options = {})
  foo = options[:foo] || true
  bar = options[:bar] || :true
  puts "foo is #{foo}, bar is #{bar}"
end

>> test(:foo => false, :bar => :false)
foo is true, bar is false

I've only tried this using Ruby 1.8.7.

© Stack Overflow or respective owner

Related posts about ruby