Ok - I know how to get the fields of an Openstruct object like so
class OpenStruct
def fields
@table.keys.map{|k| k.to_s}
end
end
a = OpenStruc.new()
a.foo = 1
a.bar = 2
a.fields -> ["foo", "bar"]
Great - ok how do I WORK with these once I have them?
call doesn't work [] doesn't work...
I KNOW this is one of those obvious ones that I am somehow
overlooking...
Please tell me the simple straightforward thing I am missing here...
···
--
Posted via http://www.ruby-forum.com/.
got it
ff = s.fields
ff.each{|g|
puts s.instance_eval g
}
1
2
Terry Smith wrote:
···
Ok - I know how to get the fields of an Openstruct object like so
class OpenStruct
def fields
@table.keys.map{|k| k.to_s}
end
end
a = OpenStruc.new()
a.foo = 1
a.bar = 2
a.fields -> ["foo", "bar"]
Great - ok how do I WORK with these once I have them?
call doesn't work doesn't work...
I KNOW this is one of those obvious ones that I am somehow
overlooking...
Please tell me the simple straightforward thing I am missing here...
--
Posted via http://www.ruby-forum.com/\.
Just a remark: if your processing is mainly bases on keys this way then a Hash might the better data structure. IMHO an OpenStruct is best used when you are lazy or do not know all the fields that you are going to use beforehand. I find this especially useful during development, you can stuff in more and more fields but maybe later change it to a regular class (or Struct generated class).
Kind regards
robert
···
On 24.08.2009 06:02, Terry Smith wrote:
got it
ff = s.fields
ff.each{|g|
puts s.instance_eval g
}
1
2
Terry Smith wrote:
Ok - I know how to get the fields of an Openstruct object like so
class OpenStruct
def fields
@table.keys.map{|k| k.to_s}
end
end
a = OpenStruc.new()
a.foo = 1
a.bar = 2
a.fields -> ["foo", "bar"]
Great - ok how do I WORK with these once I have them?
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
maybe you'd be interested in OpenHash? *self promotion*
(http://github.com/karottenreibe/ohash\)
or Mash
(
http://www.intridea.com/2008/4/12/mash-mocking-hash-for-total-poser-objects?blog=company
)
Both projects try to remove those oddities and incommodities of OpenStruct.
Greetz!
···
2009/8/24 Manuel Vila <mvila@3base.com>
Object#send is your friend:
Class: Object (Ruby 1.8.7)
--
Posted via http://www.ruby-forum.com/\.
Robert Klemme wrote:
Robert Klemme wrote:
Great - ok how do I WORK with these once I have them?
Just a remark: if your processing is mainly bases on keys this way then
a
Hash might the better data structure. IMHO an OpenStruct is best used
when
you are lazy or do not know all the fields that you are going to use
beforehand. I find this especially useful during development, you can
stuff
in more and more fields but maybe later change it to a regular class (or
Struct generated class).
I had a similar experience with using OpenStruct and Hashes and in the
end,
I derived my class from OpenStruct (build up a hash within the contructor
and create object with 'super()').
I am not sure what you are trying to convey here. Why would I want to
create a class that inherits OpenStruct and build up a Hash
internally?
The io-part of my app reads text files and creates objects from them
(csv-like files with named columns).
I use many of the Hash methods internaly, but the OpenStruct gives me all
the accessors for the api and the possibility to create objects from other
input types (eg. hashs or arrays, coming from a database access) with the
same constructor.
You are aware that OpenStruct can be constructed with a Hash argument.
If "intern" and "extern" are also separate phases you could work with
Hashes internally and convert them to OpenStruct instances when
handing off to external code.
Your Struct-example seems more elegant to me. I guess, I should start a
rewrite...
You'll be interested in method #members:
irb(main):001:0> Struct.new(:foo,:bar).new.members
=> [:foo, :bar]
Kind regards
robert
···
2009/8/24 Ralf Mueller <ralf.mueller@zmaw.de>:
2009/8/24 Ralf Mueller <ralf.mueller@zmaw.de>:
On 24.08.2009 06:02, Terry Smith wrote:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/