From: "David A. Black" Date: 2007-03-15T05:46:49+09:00 Subject: Re: When introspection doesn't... Hi -- On 3/14/07, Chad Lester wrote: > > Ruby pop quiz. How can it be that: > > foo.bar() > > is different from: > > foo.method(:bar).call() Here's one time: irb(main):001:0> class C; def method_missing(m); puts "caught!"; end; end => nil irb(main):002:0> C.new.x caught! => nil irb(main):003:0> C.new.method(:x).call NameError: undefined method `x' for class `C' > Is it possible to override the "method" method? > Can you also override the "class" method? Sure. I don't think there are any non-overridable methods in Ruby. It's all open. > From the FAQ: > ------------- > > 7.1 How does Ruby choose which method to invoke? > > Ruby binds all messages to methods dynamically. It searches first for > singleton methods in the receiver, then for methods defined in the > receiver's own class, and finally for methods defined in the receiver's > superclasses (including any modules which may have been mixed in). You > can see the order of searching by displaying Classname.ancestors, which > shows the ancestor classes and modules of ClassName. > > ----------- > If this is true, then I only need to check three places to see what > method will be called. Is there a way for me to look the same way Ruby > does? You can check the various classes and modules with instance_methods and other similar methods. > For those that are curious.... the real life example that raised this > question for me was in rails. When you have a Model with a has_many > relationship it creates an Array-like object that looks very much like > an Array, but behaves as described above (with a secret undocumented > deprecated find_all) method. You sound bitter :-) Keep in mind that class isn't really very important in Ruby. Generally you're more concerned with what a given object actually does. So while these AR collections report themselves as Arrays, the main thing is their behavior, which is kind of a souped-up Array behavior. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)