From: Rick DeNatale Date: 2010-04-30T21:30:07+09:00 Subject: Re: Switching dynamically between methods (inside modules) To me, this problem cries out for delegation rather than inheritance or module inclusion. Here's a first cut. module Language Japanese = { :konnichiwa => "こんにちは! (in Japanese language)", :sayounara => "さようなら。 (in Japanese language)" } French = { :bonjour => "Bonjour ! (in French language)", :au_revoir => "Au revoir. (in French language)" } English = { :hello =>"Hello! (in English language)", :bye => "Bye. (in English language)" } class NewBornKnowledge def self.method_missing(word, *args) "Goo Goo Gaa Gaa (in BabyTalk)" end def to_s nil end end class Knowledge def initialize(language) @language = language @dictionary = Language.const_get(language.to_sym) end def to_s @language end def method_missing(word, *args) @dictionary[word] || "that's not a word in #{@language}" end end def self.knowledge_for(language=nil) if language Knowledge.new(language) else NewBornKnowledge end end end class Person def initialize(language = nil) self.current_language = language end def method_missing(word, *args) @language_center.send(word) end def current_language=(language) @language_center = Language.knowledge_for(language) end def quit self.current_language = nil end end person = Person.new person.current_language # => "Goo Goo Gaa Gaa (in BabyTalk)" person.hello # => "Goo Goo Gaa Gaa (in BabyTalk)" person.current_language = :English person.hello # => "Hello! (in English language)" person.bonjour # => "that's not a word in English" person.quit person.hello # => "Goo Goo Gaa Gaa (in BabyTalk)" person.bonjour # => "Goo Goo Gaa Gaa (in BabyTalk)" person.konnichiwa # => "Goo Goo Gaa Gaa (in BabyTalk)" person.current_language = :French person.bonjour # => "Bonjour ! (in French language)" This also has the advantage that it can fairly easily be extended to support multilingual people. Something even a few of us Americans think is une bonne idée! -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale