[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

neatest way to extend Struct

Martin DeMello

10/22/2008 10:58:00 PM

I want a Structlike class that takes a hash constructor, so I can say

class A < MyStruct.new(*keys); end

a = A.new(:key1 => val1, :key2 => val2)

Is there any way to base this off Struct?

martin

9 Answers

Siep Korteling

10/22/2008 11:05:00 PM

0

Martin DeMello wrote:
> I want a Structlike class that takes a hash constructor, so I can say
>
> class A < MyStruct.new(*keys); end
>
> a = A.new(:key1 => val1, :key2 => val2)
>
> Is there any way to base this off Struct?
>
> martin
It'standard:

require 'ostruct'

(...)

hash = { "country" => "Australia", :population => 20_000_000 }
data = OpenStruct.new(hash)

p data # -> <OpenStruct country="Australia"
population=20000000>

(code from documentation)

hth,

Siep

--
Posted via http://www.ruby-....

Martin DeMello

10/22/2008 11:18:00 PM

0

On Wed, Oct 22, 2008 at 4:04 PM, Siep Korteling <s.korteling@gmail.com> wrote:
> Martin DeMello wrote:
>> I want a Structlike class that takes a hash constructor, so I can say
>>
>> class A < MyStruct.new(*keys); end
>>
>> a = A.new(:key1 => val1, :key2 => val2)
>>
>> Is there any way to base this off Struct?
>>
>> martin
> It'standard:
>
> require 'ostruct'

No, OpenStruct pulls in other things I don't want (the whole 'open' thing).

martin

Joel VanderWerf

10/22/2008 11:30:00 PM

0

Martin DeMello wrote:
> I want a Structlike class that takes a hash constructor, so I can say
>
> class A < MyStruct.new(*keys); end
>
> a = A.new(:key1 => val1, :key2 => val2)
>
> Is there any way to base this off Struct?
>
> martin

Something based on this, maybe? I hope Struct#members preserves order...

class A < Struct.new(:a, :b)
def initialize(h)
super *h.values_at(*self.class.members.map {|s| s.intern})
end
end

a = A.new(:a => 1, :b => 2)
p a # ==> #<struct A a=1, b=2>

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

David A. Black

10/22/2008 11:34:00 PM

0

Hi --

On Thu, 23 Oct 2008, Martin DeMello wrote:

> I want a Structlike class that takes a hash constructor, so I can say
>
> class A < MyStruct.new(*keys); end
>
> a = A.new(:key1 => val1, :key2 => val2)
>
> Is there any way to base this off Struct?

Do you need to use inheritance, as opposed to just instantiating
MyStruct directly? I'm thinking of, for example:

class MyStruct < Struct
def self.new(*keys)
s = super
s.class_eval do
define_method(:initialize) do |hash|
hash.each {|k,v| send("#{k}=",v) }
end
end
s
end
end

A = MyStruct.new(:a,:b)
a = A.new(:a => 1, :b => 2)
p a.b # 2


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!

Martin DeMello

10/22/2008 11:43:00 PM

0

On Wed, Oct 22, 2008 at 4:34 PM, David A. Black <dblack@rubypal.com> wrote:
>
> Do you need to use inheritance, as opposed to just instantiating
> MyStruct directly? I'm thinking of, for example:
>
> class MyStruct < Struct
> def self.new(*keys)
> s = super
> s.class_eval do
> define_method(:initialize) do |hash|
> hash.each {|k,v| send("#{k}=",v) }
> end
> end
> s
> end
> end

Doh - of course, since you aren't inheriting from Struct, you
shouldn't be inheriting from MyStruct either :) Wasn't thinking
clearly enough about the problem. Thanks!

martin

Robert Klemme

10/23/2008 6:03:00 AM

0

On 23.10.2008 01:42, Martin DeMello wrote:
> On Wed, Oct 22, 2008 at 4:34 PM, David A. Black <dblack@rubypal.com> wrote:
>> Do you need to use inheritance, as opposed to just instantiating
>> MyStruct directly? I'm thinking of, for example:
>>
>> class MyStruct < Struct
>> def self.new(*keys)
>> s = super
>> s.class_eval do
>> define_method(:initialize) do |hash|
>> hash.each {|k,v| send("#{k}=",v) }
>> end
>> end
>> s
>> end
>> end
>
> Doh - of course, since you aren't inheriting from Struct, you
> shouldn't be inheriting from MyStruct either :) Wasn't thinking
> clearly enough about the problem. Thanks!

Why inheritance at all? Why do too much?

irb(main):001:0> MyStruct = Struct.new :foo, :bar do
irb(main):002:1* def initialize(h={})
irb(main):003:2> members.each {|m| self[m] = h[m.to_sym]}
irb(main):004:2> end
irb(main):005:1> end
=> MyStruct
irb(main):006:0> ms = MyStruct.new(:bar => 1, :foo => 2)
=> #<struct MyStruct foo=2, bar=1>

Kind regards

robert

Eric Hodel

10/23/2008 6:28:00 AM

0

On Oct 22, 2008, at 23:03 PM, Robert Klemme wrote:
> Why inheritance at all? Why do too much?
>
> irb(main):001:0> MyStruct = Struct.new :foo, :bar do
> irb(main):002:1* def initialize(h={})
> irb(main):003:2> members.each {|m| self[m] = h[m.to_sym]}
> irb(main):004:2> end
> irb(main):005:1> end
> => MyStruct
> irb(main):006:0> ms = MyStruct.new(:bar => 1, :foo => 2)
> => #<struct MyStruct foo=2, bar=1>

Hey, neat! I didn't know you could do that!

I've always reopened the class.


Martin DeMello

10/23/2008 6:34:00 AM

0

On Wed, Oct 22, 2008 at 11:03 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
>
> Why inheritance at all? Why do too much?

Because I'd have to repeat that for every new struct I created. But
that's a very neat trick indeed!

martin

>
> irb(main):001:0> MyStruct = Struct.new :foo, :bar do
> irb(main):002:1* def initialize(h={})
> irb(main):003:2> members.each {|m| self[m] = h[m.to_sym]}
> irb(main):004:2> end
> irb(main):005:1> end
> => MyStruct
> irb(main):006:0> ms = MyStruct.new(:bar => 1, :foo => 2)
> => #<struct MyStruct foo=2, bar=1>

Robert Klemme

10/23/2008 7:19:00 AM

0

2008/10/23 Martin DeMello <martindemello@gmail.com>:
> On Wed, Oct 22, 2008 at 11:03 PM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>>
>> Why inheritance at all? Why do too much?
>
> Because I'd have to repeat that for every new struct I created. But
> that's a very neat trick indeed!

Oh, you want another Struct implementation that does this? I wasn't
aware of this. That's easily fixed.

class MyStruct < Struct
def initialize(h={})
members.each {|m| self[m] = h[m.to_sym]}
end
end

s1 = MyStruct.new :foo, :bar
p s1.ancestors, s1.new(:bar => 123)

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end