Professional Web Applications Themes

Hash like JS Hash (code) - Ruby

Although I may never use it, I thought I'd share the following experiment. I find mildly frustrating the need to type: myHash['foo'] to access a property of my hash, when for strings myHash.foo seems perfectly logical. (At least to me, coming from JavaScript.) So I wrote the following code, which catches any methods not available for hash and treats them like keys. (Thanks to memmove on #ruby-lang for the idea of allowing setters in addition to getters.) class Hash def method_missing(meth,*args) if /=$/=~(meth=meth.id2name) then self[meth[0...-1]] = (args.length<2 ? args[0] : args) else self[meth] end end end x = { 'name'=>'Gavin', ...

  1. #1

    Default Hash like JS Hash (code)

    Although I may never use it, I thought I'd share the following
    experiment. I find mildly frustrating the need to type:
    myHash['foo']
    to access a property of my hash, when for strings
    myHash.foo
    seems perfectly logical. (At least to me, coming from JavaScript.)

    So I wrote the following code, which catches any methods not available
    for hash and treats them like keys. (Thanks to memmove on #ruby-lang for
    the idea of allowing setters in addition to getters.)



    class Hash
    def method_missing(meth,*args)
    if /=$/=~(meth=meth.id2name) then
    self[meth[0...-1]] = (args.length<2 ? args[0] : args)
    else
    self[meth]
    end
    end
    end

    x = { 'name'=>'Gavin', 'age'=>31 }
    x.weight = 171
    x.feet = ['left','right']
    puts x.name , x.weight , x.foo , x.inspect

    PRODUCES

    Gavin
    171
    nil
    {"name"=>"Gavin", "weight"=>171, "feet"=>["left", "right"], "age"=>31}
    Gavin Guest

  2. #2

    Default Re: Hash like JS Hash (code)


    "Gavin Kistner" <com> wrote in message 

    <snip>

    Cool ! Also take a look at Dan Berger's code snippet on RubyForge:

    http://rubyforge.org/snippet/detail.php?type=snippet&id=1

    -- shanko


    Shashank Guest

  3. #3

    Default Re: Hash like JS Hash (code)

    In article <8hvXb.37945$uV3.63269attbi_s51>, Gavin Kistner wrote: 
    [...] 
    [...]

    Are you familiar with the `Struct` class?

    Person = Struct.new(:name, :weight, :age, :feet)

    gavin = Person.new('Gavin', 171, 31, ['left', 'right'])
    gavin.name # -> 'Gavin'
    gavin.weight # -> 171
    gavin.age += 31 # -> 32


    --
    Tim Sutherland <co.nz>
    2004 SDKACM President
    Software Developers' Klub - the University of Auckland ACM Student Chapter
    http://www.sdkacm.com/
    Tim Guest

  4. #4

    Default Re: Hash like JS Hash (code)

    On Sun, 15 Feb 2004 02:47:07 +0000, Tim Sutherland wrote:
     
    > [...] 
    > [...]
    >
    > Are you familiar with the `Struct` class?
    >
    > Person = Struct.new(:name, :weight, :age, :feet)
    >
    > gavin = Person.new('Gavin', 171, 31, ['left', 'right']) gavin.name # ->
    > 'Gavin'
    > gavin.weight # -> 171
    > gavin.age += 31 # -> 32[/ref]

    Also the OpenStruct class. Check out ostruct.rb.
    Tim Guest

  5. #5

    Default Re: Hash like JS Hash (code)

    How would you achieve the same accesses with rexml elements, to access
    subelements or attributes ?

    I guess it's possible, but I'm still a ruby newbie...

    CloD

    "Tim Hunter" <rr.com> a écrit dans le message de
    news:rr.com... 
    > > [...] 
    > > [...]
    > >
    > > Are you familiar with the `Struct` class?
    > >
    > > Person = Struct.new(:name, :weight, :age, :feet)
    > >
    > > gavin = Person.new('Gavin', 171, 31, ['left', 'right']) gavin.name # ->
    > > 'Gavin'
    > > gavin.weight # -> 171
    > > gavin.age += 31 # -> 32[/ref]
    >
    > Also the OpenStruct class. Check out ostruct.rb.[/ref]


    Claude Guest

  6. #6

    Default Re: Hash like JS Hash (code)

    (second time I try to post... I did not see my post appear on the
    newslist... I hope it is not redundant, otherwise please mail me the answer)

    How would you achieve the same accesses with rexml elements, to access
    subelements or attributes ?

    I guess it's possible, but I'm still a ruby newbie...

    CloD <net>

    "Gavin Kistner" <com> a écrit dans le message de
    news:8hvXb.37945$.. 


    Claude Guest

  7. #7

    Default Re: Hash like JS Hash (code)

    Claude Brisson wrote: 

    Use the exact same code as I gave for the Hash object, but use the class
    of your choice there. (To make things really ugly, do this on Object,
    and EVERYTHING in Ruby will behave this way :p)

    If you don't need setters, but only accessors via dot notation, it can
    be simplified to:

    class SomeClass
    def method_missing(meth,*args)
    /=$/=~meth.id2name ? nil : self[meth]
    end
    end


    All this said, you should think hard before you do this, since it will
    make your code somewhat un-ruby-like; further, you run a strong risk of
    running into confusing problems, where you think you're accessing a
    property by name but are in fact invoking a method.

    --
    (-, /\ \/ / /\/
    Gavin Guest

  8. #8

    Default Re: Hash like JS Hash (code)

    Claude Brisson wrote: 

    Use the exact same code as I gave for the Hash object, but use the
    class of your choice there. (To make things really ugly, do this on
    Object, and EVERYTHING in Ruby will behave this way...at least,
    everything that supports the [] and []= methods :p)

    If you don't need setters, but only getters via dot notation, it can be
    simplified to:


    class SomeClass
    def method_missing(meth,*args)
    /=$/=~meth.id2name ? nil : self[meth]
    end
    end


    All this said, you should think hard before you do this, since it will
    make your code somewhat un-ruby-like; further, you run a strong risk of
    running into confusing problems, where you think you're accessing a
    property by name but are in fact invoking a method.

    --
    (-, /\ \/ / /\/



    Gavin Guest

  9. #9

    Default Re: Hash like JS Hash (code)

    Tim Sutherland wrote: 

    Of course I'm not familiar with it, otherwise why would I have made such
    an un-Ruby-like hack? :) [Still, it was fun an informative to do.]

    Thanks for the tip :)

    --
    (-, /\ \/ / /\/
    Gavin Guest

Similar Threads

  1. iterating through hash of hash references
    By Andrew in forum PERL Beginners
    Replies: 7
    Last Post: March 3rd, 10:49 PM
  2. hash of hash of array slices
    By Paul Kraus in forum PERL Beginners
    Replies: 6
    Last Post: January 26th, 05:55 PM
  3. Sort a hash based on values in the hash stored as arrays of hashes
    By Tore Aursand in forum PERL Miscellaneous
    Replies: 3
    Last Post: September 16th, 10:14 AM
  4. Another reference question (hash of hash references)
    By Tim Musson in forum PERL Beginners
    Replies: 2
    Last Post: August 8th, 01:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139