Forum: Ruby Hash#[]

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2006-06-11 13:46
(Received via mailing list)
Am I the only one who thinks this would be a natural behavior for
Hash#[]?

   hsh = {:foo => 'FOO', :bar => 'BAR', :baz => 'BAZ'}
   hsh[:foo]         #=> "FOO"
   hsh[:foo, :baz]   #=> ["FOO", "BAZ"]

That would enable us to do the following

   def initialize(opts = {})
     @host, @user, @pass = opts[:host, :user, :pass]
   end

Since the old functionality isn't changed, it would be backwards
compatible.

Implementation:

   class Hash
     def [](*keys)
       if keys.length == 1
         fetch(keys.first, nil)
       else
         keys.map{|key| fetch(key, nil)}
       end
     end
   end


Cheers,
Daniel
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2006-06-11 14:02
(Received via mailing list)
Daniel Schierbeck wrote:
>   class Hash
>     def [](*keys)
>       if keys.length == 1
>         fetch(keys.first, nil)
>       else
>         keys.map{|key| fetch(key, nil)}
>       end
>     end
>   end

The implementation needs a little update:

   class Hash
     def [](*keys)
       if keys.length == 1
         fetch(keys.first, default(keys.first))
       else
         keys.map{|key| fetch(key, default(key))}
       end
     end
   end

That should take care of default values and such.


Daniel
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2006-06-11 14:20
(Received via mailing list)
Daniel Schierbeck wrote:
>   class Hash
>     def [](*keys)
>       if keys.length == 1
>         fetch(keys.first, default(keys.first))
>       else
>         keys.map{|key| fetch(key, default(key))}
>       end
>     end
>   end

This was much simpler in my head...

   class Hash
     def [](*keys)
       if keys.length == 1
         fetch(keys.first) rescue default(keys.first)
       else
         keys.map{|key| fetch(key) rescue default(key)}
       end
     end
   end


Daniel
5da4c52f43677f395aff5bde775593c2?d=identicon&s=25 Daniel Schierbeck (dasch)
on 2006-06-11 14:27
(Received via mailing list)
Hash#values_at does what I want. Never mind.

!#%¤#!"%(§%§§"¤/"%/(!!!


Daniel
360243a0ed5ff6ccdbdfb584b882fc86?d=identicon&s=25 shev (Guest)
on 2006-06-11 14:30
Seems not too bad. Or am I missing something?
E1d641bfe4071a5413bac781f06d3fd1?d=identicon&s=25 Sean O'halpin (sean)
on 2006-06-11 14:36
(Received via mailing list)
On 6/11/06, Daniel Schierbeck <daniel.schierbeck@gmail.com> wrote:
> Am I the only one who thinks this would be a natural behavior for Hash#[]?
>
>    hsh = {:foo => 'FOO', :bar => 'BAR', :baz => 'BAZ'}
>    hsh[:foo]         #=> "FOO"
>    hsh[:foo, :baz]   #=> ["FOO", "BAZ"]
>
No, you're not. However, there is Hash#values_at() which already does
the same thing.
(Can anyone remind me why Hash#[] doesn't allow *args?)

Regards,
Sean
This topic is locked and can not be replied to.