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
"こんにちは! (from #{@current_language} instance variable)"
end
def sayounara
"さようなら。"
end
end
module French
def bonjour
"Bonjour ! (from #{@current_language} instance variable)"
end
def au_revoir
"Au revoir."
end
end
module English
def hello
"Hello! (from #{@current_language} instance variable)"
end
def bye
"Bye."
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.
But, I believe that it is not possible to write something like this:
class Person
include "Language::#{@current_language}" unless @current_language.nil?
end
According to you, what should be the best practice to do so?
Any comments and messages are welcome. Thank you.
Regards