I printed a collection of objects that implemented the method_missing
method. The following example demonstrates this:
class Foo
def method_missing(m, *args, &block)
puts "missing method in #{self.class} #{m}"
super
end
end
bar = [Foo.new, Foo.new, Foo.new]
bar.each { |i| puts "#{i}" }
puts "====================="
bar.each { |i| puts i }
puts "====================="
Foo.new.to_ary
Output
#<Foo:0x00000000d62ba8>
#<Foo:0x00000000d62b80>
#<Foo:0x00000000d62b58>
=====================
missing method in Foo to_ary
#<Foo:0x00000000d62ba8>
missing method in Foo to_ary
#<Foo:0x00000000d62b80>
missing method in Foo to_ary
#<Foo:0x00000000d62b58>
=====================
missing method in Foo to_ary
./missing_test.rb:6:in `method_missing': undefined method `to_ary' for #<Foo:0x00000000d61e60> (NoMethodError)
from ./missing_test.rb:16:in `<main>'
- Why is
to_ary
called? (I think it has something to do with the wayputs
tries to dig down and provide some info recursively.) - Why does the call to
to_ary
(generated as a result ofputs
) not generate amissing_method
error? It is not supported byFoo
(orObject
). All mymethod_missing
method does is print the method name and then callsuper
, so ifto_ary
shows up as missing, the call tosuper
should throw amethod_missing
exception, right?