Sorry, I just realized this is not what you want.
Although if you know the order of the attributes defined by the
Struct, you can build something upon this, transforming the hash into
an array in the appropriate order. Whether that's cleaner than your
solution is not so clear.
Jesus.
···
2011/4/28 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:
On Thu, Apr 28, 2011 at 7:00 PM, Brian Candler <b.candler@pobox.com> wrote:
I just want to check I've not missed something here. Is there a built-in
way to initialize a Struct from a hash of key/value pairs?
That is, can I shorten the following?
K1 = Struct.new :foo, :bar
module FixStruct
def set(h)
h.each { |k,v| self[k] = v }
self
end
end
class K1
include FixStruct
end
k1 = K1.new.set(:bar=>456, :foo=>123)
p k1
(I'm talking about real Struct here, not OpenStruct etc)
ruby-1.8.7-p334 :001 > K = Struct.new :id,:timestamp
=> K
ruby-1.8.7-p334 :002 > K[3,8]
=> #<struct K id=3, timestamp=8>
Tangentially, I wonder if anything like the following (very rough proof of concept) has been used instead of Struct.
class Hash
def structify!
keys.each do |key|
class << self; self; end.class_eval do
define_method key do
fetch key
end
define_method "#{key}=" do |val|
store key, val
end
end
end
end
end
h = {:foo => 1, :bar => 2}
h.structify!
p h.foo # 1
h.foo = 3
p h.foo # 3
p h # {:foo=>3, :bar=>2}
p h.oof # undefined
···
On 04/28/2011 01:24 PM, Brian Candler wrote:
Thanks for all the feedback, at least I know I hadn't missed something
obvious
But the issue with your approach is that it is not dynamic. Keys
added or removed after call to #structify! will not be taken care of.
A more dynamic approach would be
module HashStruct
def method_missing(s,*a,&b)
case
when a.empty? && key?(s)
self[s]
when a.size == 1 && /\A(.+)=\z/ =~ s
self[$1.to_sym] = a.first
else
super
end
end
end
h.extend HashStruct
h.foo
Kind regards
robert
···
On Thu, Apr 28, 2011 at 10:50 PM, Joel VanderWerf <joelvanderwerf@gmail.com> wrote:
Tangentially, I wonder if anything like the following (very rough proof of
concept) has been used instead of Struct.
class Hash
def structify!
keys.each do |key|
class << self; self; end.class_eval do
define_method key do
fetch key
end
define_method "#{key}=" do |val|
store key, val
end
end
end
end
end
h = {:foo => 1, :bar => 2}
h.structify!
p h.foo # 1
h.foo = 3
p h.foo # 3
p h # {:foo=>3, :bar=>2}
p h.oof # undefined
Tangentially, I wonder if anything like the following (very rough proof
of concept) has been used instead of Struct.
That's neat. However you'd have to make sure you've set a value for
every key of interest, including defaults, before calling "structify!"
How about this variation:
class StructHash < Hash
def initialize(h = {})
replace(self.class::DEFAULTS.merge(h))
end
end
def StructHash(defaults)
k = Class.new(StructHash)
k.const_set(:DEFAULTS, defaults)
defaults.keys.each do |key|
k.class_eval do
define_method key do
fetch key
end
define_method "#{key}=" do |val|
store key, val
end
end
end
k
end
Foo = StructHash(:foo=>123, :bar=>456)
f = Foo.new(:foo=>0)
p f
p f.foo
p f.bar
I'm usually not a fan of subclassing core types, but I could be
persuaded here.
Perhaps the initialize function should look like this instead:
class StructHash < Hash
def initialize(h = {})
replace(self.class::DEFAULTS)
h.each { |k,v| send("#{k}=", v) }
end
end
It's not as fast, but it will catch errors if you try to set
non-existent members, and it lets you use symbols and strings
interchangeably.
That's intended: #structify is supposed to turn a hash into something that looks like a Struct, not an OpenStruct.
···
On 04/29/2011 01:06 AM, Robert Klemme wrote:
On Thu, Apr 28, 2011 at 10:50 PM, Joel VanderWerf > <joelvanderwerf@gmail.com> wrote:
Tangentially, I wonder if anything like the following (very rough proof of
concept) has been used instead of Struct.
class Hash
def structify!
keys.each do |key|
class<< self; self; end.class_eval do
define_method key do
fetch key
end
define_method "#{key}=" do |val|
store key, val
end
end
end
end
end
h = {:foo => 1, :bar => 2}
h.structify!
p h.foo # 1
h.foo = 3
p h.foo # 3
p h # {:foo=>3, :bar=>2}
p h.oof # undefined
But the issue with your approach is that it is not dynamic. Keys
added or removed after call to #structify! will not be taken care of.
A more dynamic approach would be