lasitha
3/2/2009 5:20:00 AM
On Mon, Mar 2, 2009 at 2:27 AM, Michael Malone
<michael.malone@tait.co.nz> wrote:
> On Mon, Mar 2, 2009 at 2:03 AM, abc <arcadiorubiogarcia@gmail.com> wrote:
>>
>> how would you forward calls from class methods to a class atribute?
>> [...]
>>
>> I have to delegate a fairly large number of calls to an attribute. [...]
>
> class Klass
> extend Forwardable
> def_delegators :@object :method1, :method2, :method3
>
> def initialize
> @object = SomeKlass.new
> end
> end
No, that would forward to an instance variable and only work with
Klass instances.
If the OP doesn't mind using a singleton class variable instead of a
class variable [1], then we can still use forwardable:
class Foo
@hash = {}
class << self
extend Forwardable
def_delegators :@hash, :[], :[]=, :size
end
end
Foo[:bar] = 'baz'
Foo.size #=> 1
Solidarity,
lasitha
[1] Generally a good idea anyway, right?