[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Is there a library/mechanism for an object/hash hybrid?

David T

3/30/2007 6:37:00 PM

Hi,

There are times when I want to store items in a hash, and yet refer
the items
using class/object method syntax.

For example, I may want to do this:
myhash['aaa'] = 'bbb'
and then be able to refer to the 'aaa' element either as
myhash['aaa']
--or--
myhash.aaa

I started writing my own class to do this, but I wanted to check to
see if anyone had
already done so.

What do you think?

Thanks,

David

7 Answers

Guest

3/30/2007 6:41:00 PM

0

dtrusty@my-deja.com schrieb:
> Hi,
>
> There are times when I want to store items in a hash, and yet refer
> the items
> using class/object method syntax.
>
> What do you think?
http://ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenS...

regards
Jan

Gary Wright

3/30/2007 7:10:00 PM

0


On Mar 30, 2007, at 2:45 PM, Jan Friedrich wrote:

> dtrusty@my-deja.com schrieb:
>> Hi,
>>
>> There are times when I want to store items in a hash, and yet refer
>> the items
>> using class/object method syntax.
>>
>> What do you think?
> http://ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenS...

OpenStruct doesn't define access via the indexing method:

info = OpenStruct.new

info.name = 'Gary'
info[:name] # undefined method


Gary Wright




Tim Hunter

3/30/2007 7:31:00 PM

0

Gary Wright wrote:
> OpenStruct doesn't define access via the indexing method:
>
> info = OpenStruct.new
>
> info.name = 'Gary'
> info[:name] # undefined method


Hal Fulton wrote a class called "SuperStruct". Maybe it's what you're
looking for: http://sstruct.ruby....

--
Posted via http://www.ruby-....

Trans

3/30/2007 7:48:00 PM

0

Facet's OpenObject

gem install facets

require 'facet/openobject'

oo = OpenObject.new

oo.a = 1

oo[:a] #=> 1

T.


Guest

3/30/2007 8:11:00 PM

0

Gary Wright schrieb:
> OpenStruct doesn't define access via the indexing method:
>
> info = OpenStruct.new
>
> info.name = 'Gary'
> info[:name] # undefined method
It's Ruby, define it. :)
class OpenStruct
def [] arg
self.send arg
end


regards,
Jan

Gary Wright

3/30/2007 9:23:00 PM

0


On Mar 30, 2007, at 4:15 PM, Jan Friedrich wrote:

> Gary Wright schrieb:
>> OpenStruct doesn't define access via the indexing method:
>>
>> info = OpenStruct.new
>>
>> info.name = 'Gary'
>> info[:name] # undefined method
> It's Ruby, define it. :)

Of course. I was just pointing out that OpenStruct didn't
actually provide the features that the OP had asked for.

You can always roll your own.


Gary Wright




Rick DeNatale

3/30/2007 11:31:00 PM

0

On 3/30/07, dtrusty@my-deja.com <dtrusty@my-deja.com> wrote:
> Hi,
>
> There are times when I want to store items in a hash, and yet refer
> the items
> using class/object method syntax.
>
> For example, I may want to do this:
> myhash['aaa'] = 'bbb'
> and then be able to refer to the 'aaa' element either as
> myhash['aaa']
> --or--
> myhash.aaa
>
> I started writing my own class to do this, but I wanted to check to
> see if anyone had
> already done so.

I just read about this in a blog yesterday:
http://www.oreillynet.com/ruby/blog/2007/03/camping_hashwithreallyind...

The camping framework has a HashWithIndifferentAccess which does this.

Not to be confused with the class HashWithIndifferentAccess in Rails
(actually in ActiveSupport)

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...