Short story:
"Why does defined?(59[0][:whatever])
evaluate to true?"
Long story:
I came across some strange behaviour lately which threw me off.
I was developing a method that did some washing of the data:
#Me washing input data:
def foo(data)
unless data && defined?(data[0]) && defined?(data[0][:some_param])
method2(data[0][:some_param])
else
freak_out()
end
end
I usually write tests where I spew in all kinds of junk data to make sure nothing strange happens:
describe "nice description" do
it "does not call method2 on junk data" do
expect(some_subject).to_not receive(:method2)
random_junk_data_array.each do |junk|
some_subject.foo(junk)
end
end
end
Well, method2
was called here. It happened when junk
was a fixnum.
I am using ruby 2.1.0
, and I can see that Fixnum
has a #[]
method which fetches the bit at that position, nice.
But why is fixnum[0][:some_param]
considered to be defined
?