开发者

How to convert a ruby integer into a symbol

I have a Ruby array like this

q_id = [1,2,3,4,5,...,100]

I want to iterate through the array and convert into a hash like this

{
  :1 => { #some hash} ,  
  :2 => { #another hash},
  ...
  :100 => {#yet ano开发者_开发百科ther hash}
}

What is the shortest and most elegant way to accomplish this?

[EDIT : the to_s.to_sym while being handy is not how I want it. Apologies for not mentioning it earlier.]


For creating a symbol, either of these work:

42.to_s.to_sym
:"#{42}"

The #inspect representation of these shows :"42" only because :42 is not a valid Symbol literal. Rest assured that the double-quotes are not part of the symbol itself.

To create a hash, there is no reason to convert the keys to symbols, however. You should simply do this:

q_id = (1..100).to_a
my_hash_indexed_by_value = {}
q_id.each{ |val| my_hash_indexed_by_value[val] = {} }

Or this:

my_hash = Hash[ *q_id.map{ |v| [v,{}] }.flatten ]

Or this:

# Every time a previously-absent key is indexed, assign and return a new hash
my_hash = Hash.new{ |h,val| h[val] = {} }

With all of these you can then index your hash directly with an integer and get a unique hash back, e.g.

my_hash[42][:foo] = "bar"

Unlike JavaScript, where every key to an object must be a string, Hashes in Ruby accept any object as the key.


To translate an integer into a symbol, use to_s.to_sym .. e.g.,:

1.to_s.to_sym

Note that a symbol is more related to a string than an integer. It may not be as useful for things like sorting anymore.


Actually "symbol numbers" aren't a thing in Ruby (try to call the to_sym method on a number). The benefit of using symbols in a hash is about performance, since they always have the same object_id (try to call object_id on strings, booleans, numbers, and symbols).

Numbers are immediate value and, like Symbol objects, they always have the same object_id.

Anyway, using the new hash syntax implies using symbols as keys, but you can always use the old good "hash rocket" syntax

awesome_hash = { 1 => "hello", 2 => "my friend" }

Read about immediate values here:

https://books.google.de/books?id=jcUbTcr5XWwC&pg=PA73&lpg=PA73&dq=immediate+values+singleton+method&source=bl&ots=fIFlAe8xjy&sig=j7WgTA1Cft0WrHwq40YdTA50wk0&hl=en&sa=X&ei=0kHSUKCVB-bW0gHRxoHQAg&redir_esc=y#v=onepage&q&f=false


If you are creating a hard-coded constant numeric symbol, there's a simpler way:

:'99'

This produces the same results as the more complex methods in other answers:

irb(main):001:0> :'99'
=> :"99"
irb(main):002:0> :"#{99}"
=> :"99"
irb(main):003:0> 99.to_s.to_sym
=> :"99"

Of course, this will not work if you're dynamically creating a symbol from a variable, in which case one of the other two approaches is required.


As already stated, :1 is not a valid symbol. Here's one way to do what you're wanting, but with the keys as strings:

Hash[a.collect{|n| [n.to_s, {}] }]


An array of the objects you want in your hash would be so much easier to use, wouldn't it? Even a hash of integers would work pretty well, wouldn't it?


u can use 1.to_s.to_sym

but this will make symbols like :"1"


You can make symbolic keys with Hash[]:

a = Hash[(1..100).map{ |x| ["#{x}".to_sym, {}] }]

Check type of hash keys:

puts a.keys.map(&:class)

=>
Symbol
...
Symbol
Symbol
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜