P
Paul A.
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):
Suppose that languages are the following:
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:
According to you, what could be the best practice to do so?
Any comments and messages are welcome. Thank you.
Regards
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):
Suppose that languages are the following:
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:
According to you, what could be the best practice to do so?
Any comments and messages are welcome. Thank you.
Regards