___  ___    _ _    _  _ _____   _____
 / __|/ _ \  | | |  | || |_ _\ \ / / __|
| (_ | (_) | |_  _| | __ || | \ V /| _|
 \___|\___/    |_|  |_||_|___| \_/ |___|

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Ruby Programming Tutorial - Lesson 28 - Hashes in Ruby

BY: @bilal-haider | CREATED: March 19, 2018, 9:09 p.m. | VOTES: 29 | PAYOUT: $11.26 | [ VOTE ]

[IMAGE: https://steemitimages.com/DQmWkNLYQt6ivJo2abqBBSvWrJMQ6gASZBL5fac6Wv87JVT/main-qimg-fbc470474c857cb32a07817132429914.png]

Hashes

A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type.

Hash is a Class that ruby provides us to create Key-Value pairs.

_myData = Hash.new
_myData['name']    = "Bilal Haider"
_myData['age']     = 27
_myData['balance'] = 0

## using keys to access value
puts _myData['name']
puts _myData['age']
puts _myData['balance']

## using loop to print key value  pairs

_myData.each do |h|
    print h
end

[IMAGE: https://steemitimages.com/DQmdQ9zscJz8FgdB6mKhXj6YFJaV5vVt6iGdRCC4QatgjNk/Screenshot%20from%202018-03-20%2001-50-50.png]

There are many ways to create hashes :) lets explore some of them

first method you already saw in the example above,
here is second method to create hashes

_myData = {"name" => "Bilal Haider", "age" => 27, "balance" => 0}

## using keys to access values
puts _myData['name']
puts _myData['age']
puts _myData['balance']

## using loop to print key value  pairs

_myData.each do |h|
    print h
end

[IMAGE: https://steemitimages.com/DQmRuNCweKP3fyM63nuGqBJcmcSrhK1NRqjzPyBRH5Wjb3F/Screenshot%20from%202018-03-20%2001-55-46.png]

You can see that, we are getting same results ...
Here is another Way to write hashes, that is using symbols..
everytime you see ":something" that is called a symbol

_myData = {:name => "Bilal Haider", :age => 27, :balance => 0}

## using keys to access values
puts _myData[:name]
puts _myData[:age]
puts _myData[:balance]

## using loop to print key value  pairs

_myData.each do |h|
    print h
end

[IMAGE: https://steemitimages.com/DQmPsmeP8UkUsfridtHxbzh6ZDGvbh7nNn4vid6PPLScpa6/Screenshot%20from%202018-03-20%2002-00-08.png]

Here is another Way to write hashes, This syntax is similar to json :)

_myData = {name: "Bilal Haider", age: 27, balance: 0}

## using keys to access values
puts _myData[:name]
puts _myData[:age]
puts _myData[:balance]

## using loop to print key value  pairs

_myData.each do |h|
    print h
end

[IMAGE: https://steemitimages.com/DQmSVH5UDDAMf55NJJkZ4vAkVuKJrB5wKNRTnYzSjFTvtdg/Screenshot%20from%202018-03-20%2002-02-32.png]

We have discussed 4 different ways of creating hashes, They are widely used when you are creating a web application,
we are going to create few example classes in upcoming articles, we will find out few scenarios where they can be used.
I hope you liked my article. don't forget to show thumbs up :)

If you want to learn more about Hashes
http://ruby-doc.org/core-2.2.0/Hash.html

TAGS: [ #ruby ] [ #programming ] [ #steem ] [ #steemit ] [ #dev ]

Replies

@gracefavour | March 19, 2018, 9:19 p.m. | Votes: 0 | [ VOTE ]

@codedgift, you really need to see this, it is going to be of interest to you.

@munzir9 | March 19, 2018, 9:21 p.m. | Votes: 0 | [ VOTE ]

God
Upvote me

@sanalpara | March 19, 2018, 9:41 p.m. | Votes: 0 | [ VOTE ]

hi

@virtualveggies | March 19, 2018, 9:55 p.m. | Votes: 0 | [ VOTE ]

So that is what a hash is thanks sort of new. Is that what they talk about when mining certain hash rates?

@steemitboard | March 19, 2018, 10:19 p.m. | Votes: 0 | [ VOTE ]

Congratulations @bilal-haider! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[IMAGE: https://steemitimages.com/70x80/http://steemitboard.com/notifications/posts.png] Award for the number of posts published

Click on any badge to view your own Board of Honor on SteemitBoard.

To support your work, I also upvoted your post!
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

> Upvote this notification to help all Steemit users. Learn why here!

@materialesps | March 19, 2018, 11:27 p.m. | Votes: 0 | [ VOTE ]

BUENAS NOCHES ESTA BUENO TU ARTICULO PERO MEJORARÍA CON LA EXPLICACION EN UNA APLICACION

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>