arguments into instance methods in ruby

Posted by aharon on Stack Overflow See other posts from Stack Overflow or by aharon
Published on 2010-05-08T15:16:35Z Indexed on 2010/05/08 15:18 UTC
Read the original article Hit count: 302

Filed under:

So, I'd like to be able to make a call

x = MyClass.new('good morning', 'good afternoon', 'good evening', 'good night',
            ['hello', 'goodbye'])

that would add methods to the class whose values are the values of the arguments. So now:

p x.methods #> [m_greeting, a_greeting, e_greeting, n_greeting,
                         r_greeting, ...]

And

p x.m_greeting #> "good morning"
p x.r_greeting #> ['hello', 'goodbye']

I realize that this is sort of what instance variables are to do (and that if I wanted them immutable I could make them frozen constants) but, for reasons beyond my control, I need to make methods instead.

Thanks!

© Stack Overflow or respective owner

Related posts about ruby