[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Can Ruby do this magic... elegantly
From:       Robert Klemme <shortcutter () googlemail ! com>
Date:       2006-11-12 10:45:04
Message-ID: 4roc2lFs9jl0U1 () mid ! individual ! net
[Download RAW message or body]

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

Kind regards

	robert

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic