From: dblack@... Date: 2006-11-12T21:50:29+09:00 Subject: Re: Can Ruby do this magic... elegantly Hi-- On Sun, 12 Nov 2006, Robert Klemme wrote: > J2M wrote: >> I would like to be able to be able to include instance methods of a >> Struct into a class. e.g. >> >> foo = Struct.new(:attribute, :another_attribute) >> bar = foo.new >> >> class Bas >> >> some_ruby_magic >> >> end >> >> So that I can then do >> >> Bas.attribute="a value" >> Bas.attribute >> >> Kind of like doing module_functions but that doesn't work inside a >> class. > > I don't know why people make it so complicated. :-) All these are easier than > other approaches suggested so far: > > Foo = Struct.new(:attribute, :another_attribute) > class Bas < Foo > end > > class Bas < Struct.new(:attribute, :another_attribute) > end > > or even > > Bas = Struct.new(:attribute, :another_attribute) do > def another_method() end > end You're adding instance methods to Bas rather than to Bas's singleton class, though. The OP wants to do: Bas.attribute = "a value" David -- David A. Black | dblack@rubypal.com Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org