From: Robert Klemme Date: 2010-04-30T22:42:42+09:00 Subject: Re: Switching dynamically between methods (inside modules) 2010/4/30 Paul A. : > Impressive code, thanks Rick Denatale! Thank you too, Robert Dober!  I > am currently reading your solutions. > > Anyway, I don't know if delegation is the best option for my needs. > Actually, if for instance we update Person class such as: > > class Person >  attr_accessor :name > >  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 > > ...and English hash like this: > > module Language >  English = { >    :hello => "Hello! My name is #{@name}.", This does not work as the #{} is expanded right here, i.e. if you look at the map you'll likely find this: "Hello! My name is ." >    :bye => "Bye. (in English language)" >  } >  # ... > } > > ...then it's hard to access instance variables (and instance methods) > from person object: > > person = Person.new > person.name = "Paul" > > person.current_language = :English > person.hello    # => "Hello! My name is ." > > Maybe only inheritance can do it, no?  It's tricky :s No, you just need to change the contract between Person and the delegate. You can for example pass on self (i.e. the Person instance) and the delegate extracts instance variable values via instance_variable_get. You can also do it the other way round and make delegate methods expect an object which replies to particular methods (like a callback interface in other languages). Inheritance is often abused IMHO. People seem to tend to lump too much disparate functionality together in a single inheritance hierarchy. At least that's my observation. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/