Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Ruby | Array to_h() function

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Array#to_h() : to_h() is a Array class method which returns the result of interpreting ary as an array of [key, value] pairs.

Syntax: Array.to_h()

Parameter: Array

Return: the result of interpreting ary as an array of [key, value] pairs.

Example #1 :




# Ruby code for to_h() method
  
# declaring array
a = [[:foo, :bar], [1, 2]]
  
# to_h method example
puts "to_h() method form : #{a.to_h()}\n\n"

Output :

to_h() method form : {:foo=>:bar, 1=>2}

Example #2 :




# Ruby code for to_h() method
  
# declaring array
a = [[:geeks, :geeks], [1, 2]]
  
# to_h method example
puts "to_h() method form : #{a.to_h{|s| [s.ord, s]}}\n\n"

Output :

to_h() method form : {:geeks=>:geeks, 1=>2}

My Personal Notes arrow_drop_up
Last Updated : 05 Dec, 2019
Like Article
Save Article
Similar Reads