From: "Paul A." Date: 2010-04-30T19:50:09+09:00 Subject: Switching dynamically between methods (inside modules) Hi, I would like to allow a person object (instanced from a Person class) to speak a language (which is a collection of public methods stored in Language module): >> class Person >> attr_accessor :current_language >> >> def quit >> # Unselect the current language, if any: >> @current_language = nil >> end >> end Suppose that languages are the following: >> module Language >> module Japanese >> def konnichiwa >> "こんにちは! (in #{@current_language} language)" >> end >> >> def sayounara >> "さようなら。 (in #{@current_language} language)" >> end >> end >> >> module French >> def bonjour >> "Bonjour ! (in #{@current_language} language)" >> end >> >> def au_revoir >> "Au revoir. (in #{@current_language} language)" >> end >> end >> >> module English >> def hello >> "Hello! (in #{@current_language} language)" >> end >> >> def bye >> "Bye. (in #{@current_language} language)" >> end >> end >> end Example of use: person = Person.new person.current_language # => nil person.hello # => may raise a nice no method error person.current_language = :english person.hello # => "Hello! (from english instance variable)" person.bonjour # => may also raise a no method error person.quit person.current_language = :french person.bonjour # => "Bonjour ! (from french instance variable)" As you can see, a language is such as a protocol. So a person can switch on a specific protocol, but only one at a time. For modular reasons, storing each language into a module is friendly. So I think this way is the more logical Ruby way, isn't it. So I would like to merge and unmerge dynamically module's methods (with a kind of dynamic include) into the object... and then (for instance) access them with person.public_send(). Thus, instance variables like @current_language are still accessible from the new methods. But, I believe that it is not possible to write something like this: >> class Person >> # wrong code: >> include "Language::#{@current_language}" unless @current_language.nil? >> end According to you, what could be the best practice to do so? Any comments and messages are welcome. Thank you. Regards -- Posted via http://www.ruby-forum.com/.