Hi --

On Mon, 15 Nov 2004, trans.  (T. Onoma) wrote:

> Hi David,
> 
> On Sunday 14 November 2004 03:51 pm, David A. Black wrote:
> | On Mon, 15 Nov 2004, trans.  (T. Onoma) wrote:
> | > How 'bout an OpenStruct#update for adding values after initialization. Or
> | > is there another way to do?
> |
> | OpenStruct needs to be very conservative about what methods it has,
> | since the whole point of it is to allow you to make up arbitrarily
> | named members.
> 
> Why so conservative? So they don't overwrite standard object methods? I 
> wondered why accessors weren't used. I suppose a hash is faster too. Hek, 
> maybe '@' itself should be hash and forget about it ;)

I'm assuming so.  Actually here's what happens if you try:

  irb(main):020:0> o = OpenStruct.new
  => <OpenStruct>
  irb(main):021:0> o.class = 1
  => 1
  irb(main):022:0> o.class
  => OpenStruct

> | But I'm also not sure what you need to do that you can't do.  The
> | OpenStruct object should let you add values indefinitely.
> 
> This is what I mean:
> 
>   o = OpenStruct.new( foo_hash )
>   
>   # later ...
> 
>   o.update( bar_hash )
> 
> The reason is because I am modifying and using the object on the fly.

It's easy to write:

  o = OpenStruct.new({ :a => 1 })
  def o.update(h); h.each {|k,v| send("#{k}=",v)}; end
  o.update({ :a => 2})
  p o.a

  # =>  2

(Modularize and error-check as required :-)


David

-- 
David A. Black
dblack / wobblini.net