[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Multiple assignment in a hash

Stefan Lang

8/14/2006 2:43:00 AM

Anyone know of any ruby shortcuts for multiply assigning elements in a hash?

What I need to do is set up a hash like this...

{ "one" => "A", "two"=>"A", "three" => "A" }

What I would like to do is something like this....

{ "one","two","three" => "A" } (this doesn't work)

_Kevin
www.sciwerks.com
--
Posted with http://De.... Sign up and save your mailbox.

5 Answers

Logan Capaldo

8/14/2006 2:55:00 AM

0


On Aug 13, 2006, at 10:43 PM, Kevin Olbrich wrote:

> Anyone know of any ruby shortcuts for multiply assigning elements
> in a hash?
>
> What I need to do is set up a hash like this...
>
> { "one" => "A", "two"=>"A", "three" => "A" }
>
> What I would like to do is something like this....
>
> { "one","two","three" => "A" } (this doesn't work)
>

keys = %w(one two three)
hash = Hash[ *keys.zip(Array.new(keys.length){"A"}).flatten ]

Or maybe in this case the default is good enough?

hash = Hash.new { |h,k| h[k] = "A" }
hash.values_at("one", "two", "three")
p hash


> _Kevin
> www.sciwerks.com
> --
> Posted with http://De.... Sign up and save your mailbox.
>


Jeff Schwab

8/14/2006 3:33:00 AM

0

Kevin Olbrich wrote:
> Anyone know of any ruby shortcuts for multiply assigning elements in a hash?
>
> What I need to do is set up a hash like this...
>
> { "one" => "A", "two"=>"A", "three" => "A" }
>
> What I would like to do is something like this....
>
> { "one","two","three" => "A" } (this doesn't work)

Well, not a shortcut per se...

h = {}
["one", "two", "three"].each {|k| h[k] = "A"}

Robin Stocker

8/14/2006 11:48:00 AM

0

Logan Capaldo wrote:
> keys = %w(one two three)
> hash = Hash[ *keys.zip(Array.new(keys.length){"A"}).flatten ]

Why complicated when you could do it easier and clearer:

hash = {}
%w(one two three).each{ |k| hash[k] = "A" }

Robin

Logan Capaldo

8/14/2006 1:46:00 PM

0


On Aug 14, 2006, at 7:47 AM, Robin Stocker wrote:

> Logan Capaldo wrote:
>> keys = %w(one two three)
>> hash = Hash[ *keys.zip(Array.new(keys.length){"A"}).flatten ]
>
> Why complicated when you could do it easier and clearer:
>
> hash = {}
> %w(one two three).each{ |k| hash[k] = "A" }
>
> Robin
>
I dunno. Didn't think of it at the time.

Daniel Schierbeck

8/14/2006 5:02:00 PM

0

Kevin Olbrich wrote:
> Anyone know of any ruby shortcuts for multiply assigning elements in a hash?
>
> What I need to do is set up a hash like this...
>
> { "one" => "A", "two"=>"A", "three" => "A" }
>
> What I would like to do is something like this....
>
> { "one","two","three" => "A" } (this doesn't work)

Perhaps this?

class Hash
def []= (*keys)
value = keys.pop
keys.each{|key| store(key, value) }
end
end

hsh = {}
hsh[:a, :b, :c] = 42
hsh #=> {:a => 42, :b => 42, :c => 42}

Do remember that all keys will have the *same* value, so:

hsh[:a, :b] = "foo"
hsh[:a].upcase!
hsh[:b] #=> "FOO"


Cheers,
Daniel