Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Can Ruby do this magic... elegantly

Reply
Thread Tools

Can Ruby do this magic... elegantly

 
 
J2M
Guest
Posts: n/a
 
      11-12-2006
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.
Thanks,
James


 
Reply With Quote
 
 
 
 
Austin Ziegler
Guest
Posts: n/a
 
      11-12-2006
On 11/11/06, 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.


class Bas
@__struct__ = Struct.new(:attribute, :another_attribute)

class << self
def method_missing(sym, *args)
@__struct__.send(sym, *args) if @__struct__.respond_to?(sym)
end
end
end

That should work.

-austin
--
Austin Ziegler * * http://www.halostatue.ca/
* * http://www.halostatue.ca/feed/
*

 
Reply With Quote
 
 
 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      11-12-2006
On Sun, 12 Nov 2006, 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.
> Thanks,
> James



harp:~ > cat a.rb
class Module
def struct_attrs struct
singleton_class = class << self
self
end
singleton_class.module_eval{ struct.members.each{|m| attr_accessor m} }
end
end

class Bas
foo = Struct.new :attribute, :another_attribute
bar = foo.new

struct_attrs foo
end

Bas.attribute = "a value"
p Bas.attribute


harp:~ > ruby a.rb
"a value"



-a
--
my religion is very simple. my religion is kindness. -- the dalai lama

 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      11-12-2006

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.


Foo = Struct.new(:attribute, :another_attribute)

class Bas
extend Foo.to_module
end

Ha! Only if it were so easy! Actaully if one had access to Ruby's
source it would rather trivial (hint). In anycase to par down Ara's
solution to it's core:

Foo = Struct.new :attribute, :another_attribute

class Bas
class << self
attr_accessor *Foo.members
end
end

Note the use of the constant which eases access by avoiding
(class<<self;self;end).class_eval.

T.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-12-2006
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
 
Reply With Quote
 
J2M
Guest
Posts: n/a
 
      11-12-2006
Robert, T, A & Austin;

A plethora of choices. All while I slept too

Thank you all.
James


 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      11-12-2006
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 |
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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-12-2006
wrote:
> 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"


Oh, ok then I misinterpreted that. I read "include instance methods
into a class" as including them as instance methods. My bad. Sorry for
the noise.

In this particular case, /if/ the aim is to define attribute accessors a
direct definition is probably the easiest solution

class Bas
class <<self
attr_accessor :attribute, :another_attribute
end
end

Regards

robert
 
Reply With Quote
 
J2M
Guest
Posts: n/a
 
      11-12-2006
I got the final solution down to this which I think is rather elegant;

class Bas < Struct.new :attribute, :another_attribute
class << self
attr_accessor *Bas.members
end
end

I love this languate; and now really appreciate the power of the
singleton class.

Thanks.


 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      11-12-2006

J2M wrote:
> I got the final solution down to this which I think is rather elegant;
>
> class Bas < Struct.new :attribute, :another_attribute
> class << self
> attr_accessor *Bas.members
> end
> end


class Bas < Struct.new :attribute, :another_attribute
class << self
attr_accessor *members
end
end

Do you realize that you are adding #attribute and #another_attribute at
both the instance level and the class level, and niether will reference
tha same values? I.e.

Bas.attribute = 1
bas = Bas.new
bas.attribute = 2
Bas.attribute #=> 1
bas.attribute #=> 2

T.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Coding challenge: Can you parse the command line args more elegantly? jrwats Perl Misc 5 12-19-2008 06:57 PM
reading hex values elegantly from a datagram Dave Python 2 08-26-2003 05:10 PM
Re: properties + types, implementing meta-class desciptors elegantly? Michele Simionato Python 15 07-25-2003 11:48 PM
Re: properties + types, implementing meta-class desciptors elegantly? Michael Hudson Python 3 07-22-2003 02:06 AM
Active menus: how can I do this elegantly? Ken Fine ASP .Net 0 07-19-2003 04:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57