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

Discussion in 'Ruby' started by dtrusty@my-deja.com, Mar 30, 2007.

  1. Guest

    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
     
    , Mar 30, 2007
    #1
    1. Advertising

  2. Jan Friedrich, Mar 30, 2007
    #2
    1. Advertising

  3. Gary Wright Guest

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

    > 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/OpenStruct.html


    OpenStruct doesn't define access via the indexing method:

    info = OpenStruct.new

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


    Gary Wright
     
    Gary Wright, Mar 30, 2007
    #3
  4. Tim Hunter Guest

    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.rubyforge.org/.

    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Hunter, Mar 30, 2007
    #4
  5. Trans Guest

    Facet's OpenObject

    gem install facets

    require 'facet/openobject'

    oo = OpenObject.new

    oo.a = 1

    oo[:a] #=> 1

    T.
     
    Trans, Mar 30, 2007
    #5
  6. 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
     
    Jan Friedrich, Mar 30, 2007
    #6
  7. Gary Wright Guest

    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
     
    Gary Wright, Mar 30, 2007
    #7
  8. On 3/30/07, <> 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_hashwithreallyindiffer.html

    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.denhaven2.com/

    IPMS/USA Region 12 Coordinator
    http://ipmsr12.denhaven2.com/

    Visit the Project Mercury Wiki Site
    http://www.mercuryspacecraft.com/
     
    Rick DeNatale, Mar 31, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A.M

    Hybrid Project

    A.M, Jan 25, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    532
  2. Patrick Meuser

    Tomcat Hybrid/Viral Harvesting

    Patrick Meuser, Aug 16, 2003, in forum: Java
    Replies:
    2
    Views:
    480
    bznutz
    Aug 17, 2003
  3. Markus Seibold
    Replies:
    5
    Views:
    673
    Andy Dingley
    Nov 13, 2003
  4. rp
    Replies:
    1
    Views:
    557
    red floyd
    Nov 10, 2011
  5. Pokkai Dokkai
    Replies:
    8
    Views:
    141
    Phrogz
    Nov 18, 2007
Loading...

Share This Page