How to convert Array to Hash in Ruby? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to convert an array to a hash in Ruby. Converting an array to a hash can be useful when we have data in an array format and need to organize it into key-value pairs for easier access and manipulation. Table of Content Converting array to hash using Hash::[] constructorConverting array to hash using Array#each_slice Converting array to hash using Hash#[] with a blockConverting an array to hash using Hash::[] constructorThe Hash::[] constructor method creates a hash directly from an array where each pair of elements in the array is treated as a key-value pair. Syntax: Hash[*array] Example: In this example,we convert an array into a hash , where each element in the array is transformed into a key-value pair using Hash::[] constructor Ruby # Define an array with elements representing key-value pairs array = [:a, 1, :b, 2, :c, 3] # Convert an array into a hash Using Hash::[] constructor # Convert the array directly into a hash using the Hash::[] constructor hash = Hash[*array] # Output the resulting hash puts hash.inspect # Output: {:a=>1, :b=>2, :c=>3} Output{:a=>1, :b=>2, :c=>3} Converting array to hash using Array#each_slice Array#each_slice method allows to iterate over the array in chunks where each chunk contains two elements representing a key-value pair.Then, using the Hash::[] constructor, we can convert these pairs into a hash. Syntax: Hash[*array.each_slice(2).to_a.flatten] Example: In this example we use Array#each_slice to Iterate over the array in pairs , then flatten the resulting arrays and convert them into a hash using Hash::[] Ruby # Program in ruby to convert an array into a hash using Array#each_slice and Hash::[] constructor # Define an array with elements representing key-value pairs array = [:a, 1, :b, 2, :c, 3] # Iterate over the array in pairs using each_slice(2), then flatten the resulting arrays # and convert them into a hash using Hash::[] hash = Hash[*array.each_slice(2).to_a.flatten] # Output the resulting hash puts hash.inspect # Output: {:a=>1, :b=>2, :c=>3} Output{:a=>1, :b=>2, :c=>3} Converting array to hash using Hash#[] with a block In this method we iterate over the array and processes each element individually. Then we can specify a block where you define how each element should be converted into a key-value pair. Syntax: array.each_with_object({}) { |element, hash| hash[element_key] = element_value } Example: In this example we iterates over the array in pairs using each_slice(2) and assigns each pair's elements as key-value pairs in a new hash. Ruby # Define an array with elements representing key-value pairs array = [:a, 1, :b, 2, :c, 3] # convert an array into a hash Using Hash#[] with a block # Iterate over the array in pairs using each_slice(2) # For each pair, assign the first element as the key and the second element as the value hash = array.each_slice(2).each_with_object({}) { |(key, value), h| h[key] = value } # Output the resulting hash puts hash.inspect # Output: {:a=>1, :b=>2, :c=>3} Output{:a=>1, :b=>2, :c=>3} Create Quiz Comment A abhaystriver Follow 0 Improve A abhaystriver Follow 0 Improve Article Tags : Ruby Explore OverviewRuby For Beginners3 min readRuby Programming Language (Introduction)4 min readComparison of Java with Other Programming Languages4 min readSimilarities and Differences between Ruby and C language3 min readSimilarities and Differences between Ruby and C++3 min readEnvironment Setup in Ruby3 min readHow to install Ruby on Linux?2 min readHow to install Ruby on Windows?2 min readInteresting facts about Ruby Programming Language2 min readBasicsRuby | Keywords4 min readRuby | Data Types3 min readRuby Basic Syntax3 min readHello World in Ruby2 min readRuby | Types of Variables4 min readGlobal Variable in Ruby2 min readComments in Ruby2 min readRuby | Ranges4 min readRuby Literals4 min readRuby Directories5 min readRuby | Operators11 min readOperator Precedence in Ruby2 min readOperator Overloading in Ruby5 min readRuby | Pre-define Variables & Constants5 min readRuby | unless Statement and unless Modifier2 min readControl StatementsRuby | Decision Making (if, if-else, if-else-if, ternary) | Set - 13 min readRuby | Loops (for, while, do..while, until)5 min readRuby | Case Statement3 min readRuby | Control Flow Alteration7 min readRuby Break and Next Statement2 min readRuby redo and retry Statement2 min readBEGIN and END Blocks In Ruby2 min readFile Handling in Ruby4 min readMethodsRuby | Methods3 min readMethod Visibility in Ruby3 min readRecursion in Ruby4 min readRuby Hook Methods5 min readRuby | Range Class Methods5 min readThe Initialize Method in Ruby2 min readRuby | Method overriding2 min readRuby Date and Time3 min readOOP ConceptsObject-Oriented Programming in Ruby | Set 19 min readObject Oriented Programming in Ruby | Set-28 min readRuby | Class & Object4 min readPrivate Classes in Ruby3 min readFreezing Objects | Ruby2 min readRuby | Inheritance4 min readPolymorphism in Ruby3 min readRuby | Constructors2 min readRuby | Access Control8 min readRuby | Encapsulation2 min readRuby Mixins3 min readInstance Variables in Ruby3 min readData Abstraction in Ruby3 min readRuby Static Members3 min readExceptionsRuby | Exceptions4 min readRuby | Exception handling6 min readCatch and Throw Exception In Ruby3 min readRaising Exceptions in Ruby4 min readRuby | Exception Handling in Threads | Set - 12 min readRuby | Exception Class and its Methods3 min readRuby RegexRuby | Regular Expressions3 min readRuby Search and Replace2 min readRuby ClassesRuby | Float Class7 min readRuby | Integer Class3 min readRuby | Symbol Class5 min readRuby | Struct Class5 min readRuby | Dir Class and its methods3 min readRuby | MatchData Class4 min readRuby ModuleRuby | Module4 min readRuby | Comparable Module3 min readRuby | Math Module4 min readInclude v/s Extend in Ruby2 min readCollectionsRuby | Arrays4 min readRuby | String Basics5 min readRuby | String Interpolation3 min readRuby | Hashes Basics4 min readRuby | Hash Class12 min readRuby | Blocks6 min read Like