Skip to content

Commit 6b55344

Browse files
authored
Merge pull request #23 from csalmeida/merge-methods
Merge methods
2 parents 679ccc3 + c4e6cb2 commit 6b55344

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Introduction to core features of the [Ruby](https://www.ruby-lang.org) programmi
44
**Official Documentation:** https://ruby-doc.org/
55

66
## Table of Contents
7+
<details>
8+
<summary>Click to expand contents</summary>
79

810
- [Getting Started](#getting-started)
911
- [The Interactive Ruby Shell](#the-interactive-ruby-shell)
@@ -38,6 +40,8 @@ Introduction to core features of the [Ruby](https://www.ruby-lang.org) programmi
3840
- [Map Methods](#map-methods)
3941
- [Inject Methods](#inject-methods)
4042
- [Sort Methods](#sort-methods)
43+
- [Merge Methods](#merge-methods)
44+
</details>
4145

4246
# Getting Started
4347

@@ -1409,4 +1413,40 @@ end
14091413
hash.sort do |pair_1, pair_2|
14101414
pair_1[1] <=> pair_2[1]
14111415
end
1412-
```
1416+
```
1417+
1418+
## Merge Methods
1419+
1420+
Merge methods only apply to hashes and are able to merge two of them together. Takes the keys and values of one `hash` and from another to form a new `hash`. A block can be provided to it in order to provide rules when performing that merge.
1421+
1422+
```ruby
1423+
# enumerables-and-code-blocks/merge-methods.rb
1424+
hash_1 = {:a => 2, :b => 4, :c => 6}
1425+
hash_2 = {:a => 3, :b => 4, :d => 8}
1426+
1427+
hash_1.merge(hash_2) # {:a => 3, :b => 4, :c => 6, :d => 8}
1428+
```
1429+
1430+
In the example above, two hashes were merged. There were no conflicts except with the value of `hash_1[:a]` and `hash_2[:a]` as those were different.
1431+
1432+
The merge method chose to get the value of the hash being merged in by default but this can be overridden with a code block. Ruby will make use of the code block in the case of a key conflict.
1433+
1434+
```ruby
1435+
# enumerables-and-code-blocks/merge-methods.rb
1436+
hash_1.merge(hash_2) {|key,old,new| old} # {:a => 2, :b => 4, :c => 6, :d => 8}
1437+
```
1438+
1439+
Blocks are not limited to returning those values, they can also be used to return a value based on a comparison or operation:
1440+
1441+
```ruby
1442+
# Further logic can be applied to determine which value should be returned.
1443+
puts hash_1.merge(hash_2) {|key,old,new| old < new ? old : new } # {:a => 2, :b => 4, :c => 6, :d => 8}
1444+
1445+
1446+
# Both values can be used and assigned to a key.
1447+
puts hash_1.merge(hash_2) {|key,old,new| old * new } # {:a => 6, :b => 16, :c => 6, :d => 8}
1448+
```
1449+
1450+
Since `:b` also presents a `key` conflict even if they have the same values, the block logic also applies to it.
1451+
1452+
The merge method also has a `merge!` version which replaces the contents of a hash.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!usr/bin/env ruby
2+
3+
# Merge can be performed between two hashes.
4+
hash_1 = {:a => 2, :b => 4, :c => 6}
5+
hash_2 = {:a => 3, :b => 4, :d => 8}
6+
7+
# If there's a key conflict the value from the hash being merge in takes precedence by default.
8+
puts hash_1.merge(hash_2)
9+
puts hash_2.merge(hash_1)
10+
11+
# Precedence can be changed if a code block is provided.
12+
puts hash_1.merge(hash_2) {|key,old,new| new}
13+
puts hash_1.merge(hash_2) {|key,old,new| old}
14+
15+
# Further logic can be applied to determine which value should be returned.
16+
puts hash_1.merge(hash_2) {|key,old,new| old < new ? old : new }
17+
18+
# Both values can be used and assigned to a key.
19+
puts hash_1.merge(hash_2) {|key,old,new| old * new }
20+
21+
# Hash contents can also be replaced on merge.
22+
hash_1.merge!(hash_2)
23+
puts hash_1

0 commit comments

Comments
 (0)