Skip to content

Commit d650fab

Browse files
authored
Merge pull request #323 from landeqiming666/codex/issuehunt-80-demo-docs
docs: add demos for issue 80 utility methods
2 parents ea3d8fa + ddc5cbe commit d650fab

4 files changed

Lines changed: 71 additions & 0 deletions

File tree

‎demo/core/enumerable/squeeze.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Enumerable#squeeze
2+
3+
require 'facets/enumerable/squeeze'
4+
5+
Returns a new array with consecutive duplicate entries collapsed. Unlike
6+
`Array#uniq`, duplicate values are only removed when they are adjacent.
7+
8+
[1, 2, 2, 3, 3, 2, 1].squeeze.assert == [1, 2, 3, 2, 1]
9+
10+
When the enumerable is sorted first, adjacent duplicates become grouped, so
11+
the result matches `uniq`.
12+
13+
[1, 2, 2, 3, 3, 2, 1].sort.squeeze.assert == [1, 2, 3]
14+
15+
Pass one or more values to squeeze only those values.
16+
17+
[1, 2, 2, 3, 3, 2, 1].squeeze(3).assert == [1, 2, 2, 3, 2, 1]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Enumerator::Lazy#squeeze
2+
3+
require 'facets/enumerator/lazy/squeeze'
4+
5+
Provides a lazy version of `Enumerable#squeeze`, collapsing consecutive
6+
duplicates while preserving lazy enumeration.
7+
8+
enum = [1, 2, 2, 3, 3, 2, 1].lazy.squeeze
9+
enum.to_a.assert == [1, 2, 3, 2, 1]
10+
11+
Pass values to squeeze only matching entries.
12+
13+
enum = [1, 2, 2, 3, 3, 2, 1].lazy.squeeze(3)
14+
enum.to_a.assert == [1, 2, 2, 3, 2, 1]

‎demo/core/hash/fetch_nested.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Hash#fetch_nested
2+
3+
require 'facets/hash/fetch_nested'
4+
5+
Fetches a nested value by walking a series of keys. If any key is missing,
6+
`nil` is returned instead of raising `KeyError`.
7+
8+
data = {'hello' => {'world' => 42}}
9+
data.fetch_nested('hello', 'world').assert == 42
10+
11+
data.fetch_nested('hello', 'missing').assert == nil
12+
13+
When a block is given, it is called with the requested keys when the nested
14+
path cannot be fetched.
15+
16+
fallback = data.fetch_nested('hello', 'missing') { |*keys| keys.join('.') }
17+
fallback.assert == 'hello.missing'

‎demo/core/string/rotate.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## String#rotate
2+
3+
require 'facets/string/rotate'
4+
5+
Returns a copy of the string rotated left by the given count.
6+
7+
'abcdefgh'.rotate(2).assert == 'cdefghab'
8+
9+
Negative counts rotate from the right.
10+
11+
'abcdefgh'.rotate(-2).assert == 'ghabcdef'
12+
13+
## String#rotate!
14+
15+
Performs the rotation in place.
16+
17+
string = 'abcdefgh'
18+
string.rotate!(2)
19+
string.assert == 'cdefghab'
20+
21+
string = 'abcdefgh'
22+
string.rotate!(-2)
23+
string.assert == 'ghabcdef'

0 commit comments

Comments
 (0)