What is the hash in ruby ?
Hash, hash, hash ?
- Hashes are one of the biggest property which separated another language in ruby.
- Hash was known associative array in another programming language.
- In hash, elements keep ‘key => value’ format in ruby . Like
1
| hash_example = { 'name' => 'John', 'surname' => 'doe' }
|
- Values of keys can be any type, but when you use ‘eql?’ method, it can be only a key according to each other.
1
2
3
4
5
| a = { 3 => 'three', 3.0 => 'threedotzero' }
a[3] #three
a[3.0] #threedotzero
3 == 3.0 #true
3.eql? 3.0 #false
|
- Hashes usually write with symbols, like :
1
| hash_with_symbol{ name: 'John', surname: 'Doe'}
|
- Hashes are similar to Array class, but Array uses integer as its index, hash allow to us any object type.
1
2
| hash_differences_array{ 'age' => 15, 'year' => 2014 }
array_differences_hash = [1, 2, 3]
|
In my opinion, You have an information about hashes. Do you agree with me?!
- Continue to learn!, I have an information about hash, but if I want to access only keys or values , How does it access or separate these in hash ?
For this question, solution: if you want to access to keys use
keys
method, if you want to access to values use
values
methods. Like:
1
2
3
| access_values_keys{ name: 'john', surname: 'Doe'}
access_values_keys.keys # [:name, :surname]
access_values_keys.values #['John', 'Doe']
|
- Generally, hashes are using in parameters of name,
1
2
3
4
| def parameters_name(name, age)
'#{name} : #{age}'
end
puts name 'John' , 18 # Ali : 18
|
- Hashes uses to ENUMERABLE module like arrays.
1
2
| {}.class.ancestors #show the hash ancestors.
#[Hash, Enumerable, Object, PP::ObjectMixin, Kernel, BasicObject]
|
- If you want to change from array to hash, you should use
Lastly If you want to learn more, you can visit ruby-doc .