Added each method (#27159)

* Added each method

* Rephrasing sentence
This commit is contained in:
Shafrazi
2019-02-17 16:49:26 +05:30
committed by Manish Giri
parent 9988a61c56
commit 6b4136c854

View File

@@ -34,10 +34,19 @@ You can check how many elements a hash has with the `length` method:
my_hash.length # 2
```
You can iterate through a hash using the `.each` method:
```ruby
my_hash = {:key1 => 100, :key2 => 200, :key3 => 300}
my_hash.each { |key, value| puts "#{key} is #{value}" } #=> key1 is 100
#=> key2 is 200
#=> key3 is 300
```
You can also create integers as hash key but the syntax is different from the usual one
```ruby
my_hash = {1: "value"} # will raise an exception
my_hash = {1 => "value"} # will create hash with corresponding key value pair
```
```