[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Hash key : string or symbol!?

Abir B.

4/9/2009 8:31:00 AM

Hello,
I had to manipulate a hash (delete pairs, add pairs, )

But my problem is that the data comes sometimes as string and other
times as symbol, and this makes problems in updating the hash (sometimes
I have an entry 2 times with a symbol key and string key).

for example in this code:
hash = {:a => :b, 2 => 3, "g"=> 5 }
=> {2=>3, "g"=>5, :a=>:b}
h = {:a => :b, 2 => 4, :g=> 6 }
=> {:g=>6, 2=>4, :a=>:b}
hash.merge(h)
=> {:g=>6, 2=>4, "g"=>5, :a=>:b}

I want that the value of hash["g"] changes to 6 without adding another
pair {:g => 6}

Someone has any idea?
thanks
--
Posted via http://www.ruby-....

8 Answers

Henrik Hodne

4/9/2009 8:50:00 AM

0

If you have control of adding things to the hash you could call to_s
(or to_sym) on it to force it into a string or symbol.

Hope this helps,
Henrik Hodne

On 9 Apr 2009, at 10:31, "Abir B." <abboura84@yahoo.fr> wrote:

> Hello,
> I had to manipulate a hash (delete pairs, add pairs, )
>
> But my problem is that the data comes sometimes as string and other
> times as symbol, and this makes problems in updating the hash
> (sometimes
> I have an entry 2 times with a symbol key and string key).
>
> for example in this code:
> hash = {:a => :b, 2 => 3, "g"=> 5 }
> => {2=>3, "g"=>5, :a=>:b}
> h = {:a => :b, 2 => 4, :g=> 6 }
> => {:g=>6, 2=>4, :a=>:b}
> hash.merge(h)
> => {:g=>6, 2=>4, "g"=>5, :a=>:b}
>
> I want that the value of hash["g"] changes to 6 without adding another
> pair {:g => 6}
>
> Someone has any idea?
> thanks
> --
> Posted via http://www.ruby-....
>

LAMBEAU Bernard

4/9/2009 8:53:00 AM

0

The easiest would be to decide which kind of key you really want
(String of Symbol), and ensure that you use only them.

Sorry for this a little bit sarcastic answer but Ruby's behavior is
perfectly valid here.

blambeau

On Thu, Apr 9, 2009 at 10:31 AM, Abir B. <abboura84@yahoo.fr> wrote:
> Hello,
> I had to manipulate a hash (delete pairs, add pairs, )
>
> But my problem is that the data comes sometimes as string and other
> times as symbol, and this makes problems in updating the hash (sometimes
> I have an entry 2 times with a symbol key and string key).
>
> for example in this code:
> hash = {:a => :b, 2 => 3, "g"=> 5 }
> => {2=>3, "g"=>5, :a=>:b}
> h = {:a => :b, 2 => 4, :g=> 6 }
> => {:g=>6, 2=>4, :a=>:b}
> hash.merge(h)
> => {:g=>6, 2=>4, "g"=>5, :a=>:b}
>
> I want that the value of hash["g"] changes to 6 without adding another
> pair {:g => 6}
>
> Someone has any idea?
> thanks
> --
> Posted via http://www.ruby-....
>
>

Andrew Timberlake

4/9/2009 8:55:00 AM

0

On Thu, Apr 9, 2009 at 10:31 AM, Abir B. <abboura84@yahoo.fr> wrote:
> Hello,
> I had to manipulate a hash (delete pairs, add pairs, )
>
> But my problem is that the data comes sometimes as string and other
> times as symbol, and this makes problems in updating the hash (sometimes
> I have an entry 2 times with a symbol key and string key).
>
> for example in this code:
> hash = {:a => :b, 2 => 3, "g"=> 5 }
> => {2=>3, "g"=>5, :a=>:b}
> h = {:a => :b, 2 => 4, :g=> 6 }
> => {:g=>6, 2=>4, :a=>:b}
> hash.merge(h)
> => {:g=>6, 2=>4, "g"=>5, :a=>:b}
>
> I want that the value of hash["g"] changes to 6 without adding another
> pair {:g => 6}
>
> Someone has any idea?
> thanks
> --
> Posted via http://www.ruby-....
>
>

new_hash = {}
hash.each { |k,v| new_hash[k.to_sym] = v }
h.each { |k,v| new_hash[k.to_sym] = v }


Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

"I have never let my schooling interfere with my education" - Mark Twain

Abir B.

4/9/2009 9:40:00 AM

0

Andrew Timberlake wrote:

>> h = {:a => :b, 2 => 4, :g=> 6 }
>> Posted via http://www.ruby-....
>>
>>
>
> new_hash = {}
> hash.each { |k,v| new_hash[k.to_sym] = v }
> h.each { |k,v| new_hash[k.to_sym] = v }
>

if we have nubers in the hash that we'll be converted to nil with to_sym
:
hash = {:a => :b, 2 => 3, "g"=> 5 }
=> {2=>3, "g"=>5, :a=>:b}
h = {:a => :b, 2 => 4, :g=> 6 }
=> {:g=>6, 2=>4, :a=>:b}

new_hash = {}
=> {}
hash.each { |k,v| new_hash[k.to_sym] = v }
=> {2=>3, "g"=>5, :a=>:b}
h.each { |k,v| new_hash[k.to_sym] = v }
=> {:g=>6, 2=>4, :a=>:b}
hash
=> {2=>3, "g"=>5, :a=>:b}
h
=> {:g=>6, 2=>4, :a=>:b}
new_hash
=> {:g=>6, nil=>4, :a=>:b}

And I can't control the inputs in the hash, so I must treat that by
myself
--
Posted via http://www.ruby-....

Henrik Hodne

4/9/2009 9:47:00 AM

0

You could still loop through them and make them into strings instead
of symbols.

new_hash = {}
h.each { |k,v| new_hash[k.to_s] = v }

Regards,
Henrik Hodne

On 9 Apr 2009, at 11:40, "Abir B." <abboura84@yahoo.fr> wrote:

> Andrew Timberlake wrote:
>
>>> h = {:a => :b, 2 => 4, :g=> 6 }
>>> Posted via http://www.ruby-....
>>>
>>>
>>
>> new_hash = {}
>> hash.each { |k,v| new_hash[k.to_sym] = v }
>> h.each { |k,v| new_hash[k.to_sym] = v }
>>
>
> if we have nubers in the hash that we'll be converted to nil with
> to_sym
> :
> hash = {:a => :b, 2 => 3, "g"=> 5 }
> => {2=>3, "g"=>5, :a=>:b}
> h = {:a => :b, 2 => 4, :g=> 6 }
> => {:g=>6, 2=>4, :a=>:b}
>
> new_hash = {}
> => {}
> hash.each { |k,v| new_hash[k.to_sym] = v }
> => {2=>3, "g"=>5, :a=>:b}
> h.each { |k,v| new_hash[k.to_sym] = v }
> => {:g=>6, 2=>4, :a=>:b}
> hash
> => {2=>3, "g"=>5, :a=>:b}
> h
> => {:g=>6, 2=>4, :a=>:b}
> new_hash
> => {:g=>6, nil=>4, :a=>:b}
>
> And I can't control the inputs in the hash, so I must treat that by
> myself
> --
> Posted via http://www.ruby-....
>

Andrew Timberlake

4/9/2009 9:49:00 AM

0

On Thu, Apr 9, 2009 at 11:40 AM, Abir B. <abboura84@yahoo.fr> wrote:
> Andrew Timberlake wrote:
>
>>> h = {:a => :b, 2 => 4, :g=> 6 }
>>> Posted via http://www.ruby-....
>>>
>>>
>>
>> new_hash = {}
>> hash.each { |k,v| new_hash[k.to_sym] = v }
>> h.each { |k,v| new_hash[k.to_sym] = v }
>>
>
> if we have nubers in the hash that we'll be converted to nil with to_sym
> :
Then you could use
new_hash[k.kind_of?(Numeric) k : k.to_sym] = v

Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

"I have never let my schooling interfere with my education" - Mark Twain

Phlip

1/13/2012 12:34:00 AM

0

MattB wrote:

> Obama is far from my favorite Politician.
> How has this got anything to do with Obama?

Sry - just baiting you. It's what we do here...

2849 Dead wrote:

> Bad.  Absolutely bad.  Yes, I'm enjoying the shadenfraude of watching
> it being used first to destroy the crebility of any GOP candidate to
> survive the primaries, but as long as CU stands, America has lost
> control of its elections.

Point. If we had a functioning LSM, we wouldn't NEED Super PAC money
to explain vulture capitalism to everyone.

But...

http://boingboing.net/2012/01/12/should-the-new-york-...

The New York Times' Public Editor, Arthur Brisbane, wants you to tell
them whether they should disclose in stories when subjects are clearly
lying about something:

I’m looking for reader input on whether and when New York Times news
reporters should challenge “facts” that are asserted by newsmakers
they write about. ... Is that the prevailing view? And if so, how can
The Times do this in a way that is objective and fair? Is it possible
to be objective and fair when the reporter is choosing to correct one
fact over another?

(Uh, how about you DO YOUR FUCKING JOBS, 4th Estate!)

liberal

1/13/2012 8:19:00 PM

0

On Jan 12, 6:08 pm, 2849 Dead <d...@dead.net> wrote:
> On Thu, 12 Jan 2012 14:40:15 -0800, MattB <trdell1...@gmail.com>
> wrote:
>
>
>
>
>
> >The super PAC solution
>
> >http://www.washingtonpost.com/opinions/the-super-pac-soluti......
>
> >The Post thinks that super PACs are doing “the dirty work” in politics
> >and that there is “no solution in sight” [“The power of super PACs,”
> >editorial, Jan. 10]. I suggest that the solution is to end the limits
> >on contributions to candidates by anybody.
>
> >Super PACs exist solely as a legal fiction to avoid the limits on
> >campaign contributions. Everyone knows this, and some such legal
> >fiction will always be devised as long as the fruitless effort to
> >limit contributions continues.
>
> >No limits with prompt, full disclosure is the best realistic solution.
> >We all now know that casino magnate Sheldon Adelson gave $5 million to
> >Newt Gingrich’s super PAC. If that is dirty work, voters can decide if
> >it is a reason not to vote for Gingrich.
>
> >*******
>
> >  What has this done to our election system?.  Good or Bad?
>
> Bad.  Absolutely bad.  Yes, I'm enjoying the shadenfraude of watching
> it being used first to destroy the crebility of any GOP candidate to
> survive the primaries, but as long as CU stands, America has lost
> control of its elections.


I once posted on a similar thread that Mary Matlin argued that the
wealthy must be able to contribute huge amounts of money to make up
for their much smaller numbers at the ballot box. I believe it was on
"CrossFire".


>
> --
> "So called payroll taxes aren't taxes at all"  -- Steve Canyon, trying to explain
> why millionaires don't actually pay less taxes than median income families.