[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Converting hash keys to symbols

Jamis Buck

11/2/2004 9:36:00 PM

Golf question...

Is there an elegant way of converting all of the keys of a hash to a
symbol? The solution can assume that the existing keys are either
strings or symbols. Also, if a key is a string, it may contain a dash
character which must be converted to an underscore.

Something like this, only more elegant, would be nice:

a = some_hash_of_strings_and_symbols
new_a = Hash[*a.collect { |k,v|
k = k.gsub(/-/,"_").intern if k.is_a?(String)
[k,v] }.flatten]

Any takers?

- Jamis

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck...


9 Answers

Michael Neumann

11/2/2004 9:53:00 PM

0

On Wed, Nov 03, 2004 at 06:35:59AM +0900, Jamis Buck wrote:
> Golf question...
>
> Is there an elegant way of converting all of the keys of a hash to a
> symbol? The solution can assume that the existing keys are either
> strings or symbols. Also, if a key is a string, it may contain a dash
> character which must be converted to an underscore.
>
> Something like this, only more elegant, would be nice:
>
> a = some_hash_of_strings_and_symbols
> new_a = Hash[*a.collect { |k,v|
> k = k.gsub(/-/,"_").intern if k.is_a?(String)
> [k,v] }.flatten]
>
> Any takers?

But you should not do this for random strings, as symbols are AFAIK not
GC'ed.

How about this (converts all symbols to strings, which is easier and
more secure, as strings get GC'ed):

h = Hash.new
a.each {|k,v| h[k.to_s.tr('-','_')] = v}
h

Regards,

Michael


James Britt

11/2/2004 9:56:00 PM

0

Jamis Buck wrote:
> Golf question...
>
> Is there an elegant way of converting all of the keys of a hash to a
> symbol? The solution can assume that the existing keys are either
> strings or symbols. Also, if a key is a string, it may contain a dash
> character which must be converted to an underscore.
>
> Something like this, only more elegant, would be nice:
>
> a = some_hash_of_strings_and_symbols
> new_a = Hash[*a.collect { |k,v|
> k = k.gsub(/-/,"_").intern if k.is_a?(String)
> [k,v] }.flatten]
>
> Any takers?
>
> - Jamis
>

I'd be inclined to add 'intern' to Symbol, and just call intern on
everything in the key set. If you get a Symbol, then Symbol#intern just
returns self.

And override intern in String to do character subs.

James


Jamis Buck

11/2/2004 10:01:00 PM

0

Michael Neumann wrote:

> But you should not do this for random strings, as symbols are AFAIK not
> GC'ed.

Good point. I hadn't considered that. However, for what I'm doing, there
are no more than a double-handful of unique, valid strings and symbols,
so I don't think memory is an issue.

>
> How about this (converts all symbols to strings, which is easier and
> more secure, as strings get GC'ed):
>
> h = Hash.new
> a.each {|k,v| h[k.to_s.tr('-','_')] = v}
> h

Ah, #tr is what I was looking for. Thanks! That's one step towards
making it more elegant, anyway.

Thanks, Michael!

- Jamis

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck...


Jamis Buck

11/2/2004 10:05:00 PM

0

James Britt wrote:

> I'd be inclined to add 'intern' to Symbol, and just call intern on
> everything in the key set. If you get a Symbol, then Symbol#intern just
> returns self.
>
> And override intern in String to do character subs.

Good point. However, this is something that is only occurring at one
point in the code, and opening the classes to (re)define the methods is
a lot more lines of code than I'm up for at this point. :)

- (lazy) Jamis

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck...


Florian Gross

11/2/2004 11:03:00 PM

0

Jamis Buck wrote:

> Is there an elegant way of converting all of the keys of a hash to a
> symbol? The solution can assume that the existing keys are either
> strings or symbols.

Does this help?

a = Hash.new do |hash, key|
hash.fetch(key.is_a?(Symbol) ? key.to_s : key.to_sym, nil)
end

a["one"] = "eins"
a[:two] = "zwei"

a["one"] == a[:one] # => true
a["two"] == a[:two] # => true

> Also, if a key is a string, it may contain a dash
> character which must be converted to an underscore.

Why? Symbols can contain dash characters. See :"foo-bar", %s{foo-bar},
"foo-bar".intern and "foo-bar".to_sym.

Also note that #to_sym is defined for Symbol as well.

So maybe you could also do something like this (in case the above
doesn't work for you):

result = Hash.new(&a.default_proc)
until a.empty?
key, value = *a.shift
result[key.to_sym] = value
end
a.replace(result)

Jamis Buck

11/2/2004 11:35:00 PM

0

Florian Gross wrote:
> Jamis Buck wrote:
>
>> Is there an elegant way of converting all of the keys of a hash to a
>> symbol? The solution can assume that the existing keys are either
>> strings or symbols.
>
>
> Does this help?
>
> a = Hash.new do |hash, key|
> hash.fetch(key.is_a?(Symbol) ? key.to_s : key.to_sym, nil)
> end

Clever! That's really nice. It won't quite work for me, however, since I
don't have control over the creation of the hash that I'm receiving.

>> Also, if a key is a string, it may contain a dash character which must
>> be converted to an underscore.
>
>
> Why? Symbols can contain dash characters. See :"foo-bar", %s{foo-bar},
> "foo-bar".intern and "foo-bar".to_sym.

Yah, I know. But I like to use symbols so I don't *have* to type the
quotes. :) It's a matter of convenience rather than optimization, in
this case.

Thanks for the suggestions, Florian. Thanks, especially, for pointing
out #to_sym. I wasn't aware of that, and I like it better than the
less-intuitive #intern.

- Jamis

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck...


The Revd

1/17/2013 8:04:00 PM

0

On Thu, 17 Jan 2013 20:45:00 +0100 (CET), "Anonymous Remailer
(austria)" <mixmaster@remailer.privacy.at> wrote:

[jew trash newsgroups removed]

>"Walt Hampton" <walt.hampton@att.net> wrote in news:694b7d.hd5.19.1@news.alt.net:
>
>> The companies pay the rabbis for the use of their kosher
>> symbol and then pass that overhead cost on to their customers,
>> you and me. That is how
>
>http://nizkor.org/hweb/orgs/american/adl/kosher-tax/r...
>hate.html
>
>The whole claim of the "Kosher Tax" is a FRAUD. In 1975(May 18),
>the NY
>Times reported that the cost to General Foods "Birds-Eye" Unite was
> .0000065 cents (U.S.) / item (that is 6.5 MILLIONTHS of a Cent).
>Heinz reported the cost was "so small we can't even calculated it."

They lied.

>Heinz indicated that the labeling of items as Kosher actually
>increased its market. Similary, in a Washington Post article,
>Entenmann's Inc, a US bakery, reported that when it received its
>kosher certification in 1981, its sales of its baked good
>"increased substantially."

Gullible American schmucks...

NoSpamAtAll

1/17/2013 8:08:00 PM

0

In article <46mgf8ppfui478dbdbmu053742puh8jl52@4ax.com>,
The Revd <peeling@degenerate.Grik> wrote:

> I suck ALL rectums, kosher or not!

Please take this sick stuff elsewhere.

The Revd

1/17/2013 8:30:00 PM

0

On Thu, 17 Jan 2013 20:07:39 +0000 (UTC), SmallHernia aka NoSpamAtAll
<spamnot@not.home> wrote:

> I suck ALL donkey rectums, kosher or not!

Please take this sick jew stuff elsewhere.