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 # -> <OpenStruct country="Australia"
population=20000000>

(code from documentation)

hth,

Siep

···

--
Posted via http://www.ruby-forum.com/\.

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

Hi --

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 Thu, 23 Oct 2008, Martin DeMello wrote:

--
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.rubypal.com for details and updates!

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

martin

···

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'

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

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

···

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 :slight_smile: Wasn't thinking
clearly enough about the problem. Thanks!

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

I've always reopened the class.

···

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>

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

···

On Wed, Oct 22, 2008 at 11:03 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

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>

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

···

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!

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