<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hashes in Ruby &#8211; TestAutomationLabs</title>
	<atom:link href="https://testautomationlabs.com/tag/hashes-in-ruby/feed/" rel="self" type="application/rss+xml" />
	<link>https://testautomationlabs.com</link>
	<description>Quality Matters</description>
	<lastBuildDate>Sat, 22 Feb 2025 15:08:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://testautomationlabs.com/wp-content/uploads/2023/08/imageedit_2_5514276710-150x150.png</url>
	<title>Hashes in Ruby &#8211; TestAutomationLabs</title>
	<link>https://testautomationlabs.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Mastering Hashes in Ruby: Your Guide to Efficient Data Organization</title>
		<link>https://testautomationlabs.com/hashes-in-ruby/</link>
					<comments>https://testautomationlabs.com/hashes-in-ruby/#respond</comments>
		
		<dc:creator><![CDATA[TestAutomationLabs]]></dc:creator>
		<pubDate>Mon, 28 Aug 2023 06:37:14 +0000</pubDate>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Hashes in Ruby]]></category>
		<guid isPermaLink="false">https://testautomationlabs.com/?p=332</guid>

					<description><![CDATA[In Ruby Programming, a hash is a collection of key-value pairs, similar to a dictionary in Python or an associative array in other programming languages. Each key in a hash is unique and it maps to a specific value so we call it a collection of key-value pairs. In hash, each value is assigned to [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In Ruby Programming, a hash is a collection of key-value pairs, similar to a dictionary in Python or an associative array in other programming languages. Each key in a hash is unique and it maps to a specific value so we call it a collection of key-value pairs. In hash, each value is assigned to a key using a hash rocket <code>=&gt;</code>. </p>



<figure class="wp-block-image size-full is-resized"><img fetchpriority="high" decoding="async" width="765" height="381" src="https://testautomationlabs.com/wp-content/uploads/2023/08/Screenshot-2023-08-28-at-12.13.50-PM.png" alt="Hashes in Ruby" class="wp-image-335" style="width:835px;height:auto" title="Mastering Hashes in Ruby: Your Guide to Efficient Data Organization" srcset="https://testautomationlabs.com/wp-content/uploads/2023/08/Screenshot-2023-08-28-at-12.13.50-PM.png 765w, https://testautomationlabs.com/wp-content/uploads/2023/08/Screenshot-2023-08-28-at-12.13.50-PM-300x149.png 300w" sizes="(max-width: 765px) 100vw, 765px" /></figure>



<p>Let&#8217;s see how you can create and work with hashes in Ruby.</p>



<h2 class="wp-block-heading">Creating Hashes in Ruby</h2>



<p>In Ruby, you can create a hash using curly braces <code>{}</code> and specify key-value pairs:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
# Creating a hash with initial values
person = {
  &quot;name&quot; =&gt; &quot;San&quot;,
  &quot;age&quot; =&gt; 28,
  &quot;occupation&quot; =&gt; &quot;QA Engineer&quot;
}

# Creating an empty hash
empty_hash = {}
</pre></div>


<p>You can also use <code>Hash.new</code> constructor to create a hash with a default value</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
# Creating a hash with a default value
grades = Hash.new(0)  # Default value is 0
</pre></div>


<h3 class="wp-block-heading">Accessing hash values</h3>



<p>You can access the values in hash using keys, as follows</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
puts person&#x5B;&quot;name&quot;]        # Outputs: San
puts person&#x5B;&quot;age&quot;]         # Outputs: 28
puts person&#x5B;&quot;occupation&quot;]  # Outputs: QA Engineer
</pre></div>


<h3 class="wp-block-heading">Modifying hash values</h3>



<p>Also, you can modify hash values using keys</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
person&#x5B;&quot;age&quot;] = 31       # Changing the value of the &quot;age&quot; key
person&#x5B;&quot;occupation&quot;] = &quot;Developer&quot;  # Changing the value of the &quot;occupation&quot; key
</pre></div>


<h3 class="wp-block-heading">Iterating Over Hashes:</h3>



<p>You can iterate over the keys and values in a hash using various methods like <code>each</code>, <code>each_key</code>, and <code>each_value</code>:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
person.each do |key, value|
  puts &quot;#{key}: #{value}&quot;
end
</pre></div>


<h3 class="wp-block-heading">Symbols as Keys in Ruby Hash</h3>



<p>In Ruby, it&#8217;s common to use symbols as keys in hashes, especially when the keys are not expected to change:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: ruby; title: ; notranslate">
person = {
  name: &quot;Alice&quot;,
  age: 25,
  occupation: &quot;Designer&quot;
}
</pre></div>


<h2 class="wp-block-heading">Conclusion</h2>



<p>There are many methods to play with Hashes in Ruby, some of which are <code>keys</code>, <code>values</code>, <code>merge</code>, <code>delete</code>, <code>has_key?</code>, and more. You can refer to the Ruby documentation for a comprehensive list of hash methods: <a href="https://ruby-doc.org/3.2.2/Hash.html" target="_blank" rel="noreferrer noopener">Ruby Hash Documentation</a></p>



<p>Remember that hashes are unordered collections, which means the order of key-value pairs is not guaranteed to be the same as when they were inserted.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://testautomationlabs.com/hashes-in-ruby/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
