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 another 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.]
A Ruby symbol is a thing that has both a number (integer) and a string.
Ruby symbols are created by placing a colon (:) before a word. You can think of it as an immutable string. A symbol is an instance of Symbol class, and for any given name of symbol there is only one Symbol object.
A Symbol object is created by prefixing an operator, string, variable, constant, method, class name with a colon (:). The symbol object will be unique for each different name but does not refer to a particular instance of the name, for the duration of a program's execution.
Ruby provides the to_i and to_f methods to convert strings to numbers. to_i converts a string to an integer, and to_f converts a string to a float.
Ruby Language Converting a String to Integer. Example. You can use the Integer method to convert a String to an Integer: Integer("123") # => 123 Integer("0xFF") # => 255 Integer("0b100") # => 4 Integer("0555") # => 365. You can also pass a base parameter to the Integer method to convert numbers from a certain base.
To convert a string to a symbol, use the to_sym method, like this: To take the string "First name" and convert it to the symbol :first_name, you’d lower-case all the letters and replace spaces with underscores:
You’re probably familiar with this first group of conversion methods. These methods return a new object of a specific class that represents the current object. “I want to convert the Range 1..10 into an Array that represents that range.” There are ways in which Ruby calls these conversion methods for you implicitly.
If you pass the Integer method a value that can’t be converted, Ruby will raise an error: You can then handle the error and provide a message to the user, asking them to provide better data. This approach is less convenient, but it can result in better data integrity, since your data won’t be coerced.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With