Neatest way to extend Struct

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

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 # ->

(code from documentation)

hth,

Siep

On Wed, Oct 22, 2008 at 4:04 PM, Siep K. [email protected]
wrote:

It’standard:

require ‘ostruct’

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

martin

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

On Wed, Oct 22, 2008 at 4:34 PM, David A. Black [email protected]
wrote:

 end

end
s
end
end

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

martin

On 23.10.2008 01:42, Martin DeMello wrote:

 end

end
s
end
end

Doh - of course, since you aren’t inheriting from Struct, you
shouldn’t be inheriting from MyStruct either :slight_smile: 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)
=> #

Kind regards

robert

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 # ==> #

On Oct 22, 2008, at 23:03 PM, Robert K. 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)
=> #

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

I’ve always reopened the class.

On Wed, Oct 22, 2008 at 11:03 PM, Robert K.
[email protected] 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

2008/10/23 Martin DeMello [email protected]:

On Wed, Oct 22, 2008 at 11:03 PM, Robert K.
[email protected] 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