Ruby Hash From Array

[Solved] Ruby Hash From Array | Basic - Code Explorer | yomemimo.com
Question : array to hash ruby

Answered by : luqman-a

# array = [["a", 1], ["b", 2], ["c", 3]]
array = [[:a, 1], [:b, 2], [:c, 3]]
# convert array to hash
hash = Hash[array]
puts hash
# Stdout/output:
# {:a=>1, :b=>2, :c=>3}

Source : | Last Update : Sun, 05 Jun 22

Question : ruby array to hash with index

Answered by : nguyen-quang-huy

arr = ["one", "two", "three", "four", "five"]
Hash[(0...arr.size).zip arr]
# => {0=>"one", 1=>"two", 2=>"three", 3=>"four", 4=>"five"}

Source : | Last Update : Tue, 27 Dec 22

Answers related to ruby hash from array

Code Explorer Popular Question For Basic