DEV Community

loading...
Cover image for Sharpen your Ruby: Hash

Sharpen your Ruby: Hash

Eric The Coder
Businessman and blogger #Javascript, #Python and #PHP. My favorite frameworks are #Nextjs, #Laravel, and #Django. I am also a fan of #TailwindCSS
・2 min read

I develop in Javascript, Python, PHP and Ruby. By far Ruby is my favorite programming language.

Together let start a journey and revisit our Ruby foundations.

Each post will include some theory but also exercise and solution.

If you have any questions/comments or your are new and need help, you can comment below or send me a message.

What is a Hash

A Hash is a collection of key-value pairs for example: "product" => "price".

It is similar to an Array, except that indexing is done via a keys of any object type, not an integer index like array.

Create a Hash

Here how to create a Hash in Ruby

products = {'iPhone' => 699, 'iPad' => 799, 'Macbook Pro' => 1899}
# or same but newer syntax
products = {'iPhone': 699, 'iPad': 799, 'Macbook Pro': 1899}
Enter fullscreen mode Exit fullscreen mode

That's create a Hash with 3 entry (keys / values):
iPhone : 699
iPad : 799
Macbook Pro : 1899

You can search / access any item by specifying the key name

puts products[:iPad] # 799
Enter fullscreen mode Exit fullscreen mode

You can search / access any item with default value if key not present

puts product.fetch(:iPhone, 0) # 699
puts product.fetch(:iWatch, 0) # not found so 0

Enter fullscreen mode Exit fullscreen mode

Remove item from a Hash

You can remove a item from a Hash with this syntax

products.delete(:iPhone)
puts products # {"iPad"=>799, "Macbook Pro"=>1899}
Enter fullscreen mode Exit fullscreen mode

Add item to a Hash

To add a new item just use this syntax

products[:iPad2] = 899
puts products # {"iPad"=>799, "Macbook Pro"=>1899, :iPad2=>899}
Enter fullscreen mode Exit fullscreen mode

Iterate over a Hash

To iterate over key/value pair

products.each do |key, value| 
  puts key
  puts value
end
Enter fullscreen mode Exit fullscreen mode

To iterate only over values

products.values.each do |value| 
  puts value
end
Enter fullscreen mode Exit fullscreen mode

To iterate only over keys

products.keys.each do |key| 
  puts key
end
Enter fullscreen mode Exit fullscreen mode

Array of Hash

Often you will have to work with an array of hash. Here an example:

products = [
  {id: 100, name: 'iPhone12', price: 699},
  {id: 200, name: 'iPad', price: 799},
  {id: 300, name: 'iWatch', price: 899},
]
Enter fullscreen mode Exit fullscreen mode

It's a array so you can access an item like an array

product_1 = products[0]
# {id: 100, name: 'iPhone12', price: 699}
Enter fullscreen mode Exit fullscreen mode

You can extract info from hash like this

puts products[0][:price] # 699
Enter fullscreen mode Exit fullscreen mode

Select only product with a price greater than 750$

high_price_products = products.select { | product | product[:price] > 750 }
Enter fullscreen mode Exit fullscreen mode

Conclusion

That's it for today. The journey just started, stay tune for the next post very soon. (later today or tomorrow)

If you have any comments or questions please do so here or send me a message on twitter.

I am new on twitter so if you want to make me happy
Follow me: Follow @justericchapman

Discussion (0)

Forem Open with the Forem app