From: Brian Candler Date: 2011-08-01T16:39:02+09:00 Subject: Re: A question on style. Joseph Phillips wrote in post #1014043: > def get_responses > [ > "Got it.", > "Interesting. I've made a note of that.", > "Noted.", > "Right.", > "I see.", > "As you say." > ] > end I would typically define a constant for this: class Foo GET_RESPONSES = [ "Got it.", "Etc.", ] ... end Then only one object is created, and you use it over and over again. To do this safely it's better to freeze both the array and its members: GET_RESPONSES = [ "Got it.".freeze, "Etc.".freeze, ].freeze Note: it's possible to override GET_RESPONSES in subclasses, but if you want a method defined in the superclass to dynamically pick the constant from the subclass then you need to use dynamic resolution like this: class Foo GET_RESPONSES = [...] def response(n) self.class::GET_RESPONSES[n] end end class Bar < Foo GET_RESPONSES = [...] end Regards, Brian. -- Posted via http://www.ruby-forum.com/.