From: Trans Date: 2007-08-24T13:28:57+09:00 Subject: Re: Dynamic methods On Aug 23, 6:46 pm, -j B- wrote: > I have seen examples of catching unkown methods called on a class with > method_missing. > For example: > > class Example > def initialize(...) > #... > end > > def method_missing(item) > call = item.id2name > process(call) > end def method_missing(item, *args, &blk) process(item, *args, &blk) # item is already a symbol end > end > > But I'm not sure if that is an 'acceptable' way to do things. Also, > suppose that I wanted to add a method dynamically that takes arguments, > is this even possible? > My general thought is allowing functionality to be added to a program > dynamically based on a set of rules in a config file. > Any suggestions are welcome. > Thank you. Also, keep in mind Ruby classes are open. You can add real methods to them on the fly. class Example end foo = "bar" Example.class_eval %{ def #{foo} "#{foo}" end } Example.new.foo #=> "bar" T.