On Wed, Apr 27, 2011 at 3:13 PM, ivanpoval <[email protected]> wrote:
> So in AR `super` makes method_missing to define the `ghost` attribute
> method `name` in order to reduce the method lookup next time.
> After it gets defined it also calls that method - at that time it
> should only return "bob". After it gets called in method_missing,
> shouldn't it just return back into our User#name scope?

Nope, you can't do that in ruby.  The ActiveRecord code is the closest
you can come, restarting the lookup process.

> Since the second time we call User#name we get the expected result, we
> can assume that method_missing defined the `ghost` attribute
> correctly.
> Right?

method_missing does define the method correctly, but it doesn't check
that the method was already defined before it defined attribute
methods.  If it did that, it could determine whether the method was
actually missing or whether it was called via super.

Another way to fix this (besides what Sequel does) would be to perform
the action the attribute method would have performed had it been
defined, without using send to restart the method lookup process.
That would entail copying the logic for the attribute methods into
method_missing (or something called by method_missing).

> But it is still not very clear to me why the 'Hi' gets capitalized. I
> would expect 'bob' to be repeated twice if we have the case of doubled
> method call. # Smth. like this: 'hi Bob bob'

I would expect: hi Hi Bob

Work backwards through the method chain:

generated_methods_module#name => bob
User#name -> hi Bob
ActiveRecord::AttributeMethods#method_missing -> hi Bob
User#name -> hi Hi Bob

If you actually got "hi Hi bob", I'm not sure why.

Jeremy

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to