Dynamically adding static (class) methods to a class

Hello again,

I guess this is more of a Ruby question, but here goes anyways…

I want to intercept all calls to non existent class methods and then
call an existing class method with the non existent name.

In code…

class MyModel < ActiveRecord::Base

def MyModel.method_missing(symbol, *args)
MyModel.func(symbol.id2name)
end

def MyModel.func(func_called)
puts “You called #{func_called}”
end

end

When I try this code, it gets lost in recursion.

Thanks for the help.

Please post to the comp.lang.ruby.

Thank you,

-Conrad

Actually, it turned out to be a Rails problem…

The code I originally wrote below actually works.

The problem was in my real code where I was calling
MyModel.find_by_name from within method_missing… which was causing
infinite recursion because Rails uses method_missing to implement all
the find_by_* methods.

– C

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs