How to convert String to Hash in Ruby? Last Updated : 21 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will discuss how to convert string to hash in ruby. We can convert a string to a hash through different methods ranging from parsing JSON and parsing YAML to custom string parsing. Table of Content Converting string to hash using JSON parsingConverting string to hash using YAML parsingCustom String Parsing to HashConverting string to hash using JSON parsingThe JSON.parse method parses a JSON-formatted string and returns a hash representing the parsed data. Syntax: hash = JSON.parse(string) Example: In this example, the JSON string json_string is parsed using JSON.parse, resulting in a hash containing the key-value pairs from the JSON data. Ruby require 'json' # Define a JSON string json_string = '{"name": "John", "age": 30, "city": "New York"}' # Convert JSON string to a hash hash = JSON.parse(json_string) puts hash # Output: {"name"=>"John", "age"=>30, "city"=>"New York"} Output{"name"=>"John", "age"=>30, "city"=>"New York"}Converting string to hash using YAML parsingThe YAML.safe_load method is used to parse a YAML-formatted string into a hash. Syntax: hash = YAML.safe_load(string) Example: In this example the YAML string is parsed using YAML.safe_load, resulting in a hash containing the key-value pairs from the YAML data Ruby require 'yaml' # Define a YAML string yaml_string = "---\nname: John\nage: 30\ncity: New York\n" # Convert YAML string to a hash hash = YAML.safe_load(yaml_string) puts hash # Output: {"name"=>"John", "age"=>30, "city"=>"New York"} Output{"name"=>"John", "age"=>30, "city"=>"New York"} Custom String Parsing to HashIn this method we implemented custom parsing logic to convert the string into a hash. Syntax: hash = Hash[string.split('&').map { |pair| pair.split('=') }] Example: In this example we split the string into key-value pairs using delimiters such as '&' and '=' and then converting them into a hash. Ruby # Define a custom string format string = "name=John&age=30&city=New York" # Convert custom string to a hash hash = Hash[string.split('&').map { |pair| pair.split('=') }] puts hash # Output: {"name"=>"John", "age"=>"30", "city"=>"New York"} Output{"name"=>"John", "age"=>"30", "city"=>"New York"} Comment More infoAdvertise with us Next Article How to Convert String to JSON in Ruby? A abhaystriver Follow Improve Article Tags : Ruby Similar Reads How to Convert String to JSON in Ruby? In this article, we will learn how to convert a string to JSON in Ruby. String to JSON conversion consists of parsing a JSON-formatted string into a corresponding JSON object, enabling structured data manipulation and interoperability within Ruby applications. Converting String to JSON in RubyBelow 2 min read How to Convert Hash to JSON in Ruby? Ruby Hash is an unordered set of data in key-value pairs. These are mutable and can store multiple data types. JSON stands for Javascript Object Notation and is a human-readable file format that is commonly used in web services and API calls. In this article, we will discuss how to convert a hash to 2 min read How to Convert JSON to Hash in Ruby? In this article, we will discuss how to convert JSON to hash in Ruby. We can convert JSON to hash through different methods ranging from parsing JSON and JSON.load to using YAML.safe_load Method. Table of Content Using JSON.parse MethodUsing JSON.load MethodUsing YAML.safe_load Method Using JSON.par 2 min read How to convert String to Boolean in Ruby? In this article, we will learn how to convert string to Boolean in Ruby using various methods. Letâs understand each of them with the help of examples. Table of Content Converting string to Boolean using String#casecmpConverting string to Boolean using String#downcaseConvert string to Boolean using 3 min read How to convert Array to Hash in Ruby? 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::[] const 3 min read How to convert Hash to Object in Ruby? In this article, we will discuss how to convert a hash to an object in Ruby. Converting hash to an object allows one to access hash keys as object attributes, providing them with a more intuitive way to work with data. Table of Content Using OpenStructUsing StructUsing Hash#eachUsing OpenStructOpenS 3 min read Like