Skip to content

Commit 806054e

Browse files
committed
Add section about Enumerable#lazy
1 parent 5dcdd37 commit 806054e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.adoc

+21
Original file line numberDiff line numberDiff line change
@@ -4950,6 +4950,27 @@ LOREM
49504950
"when an unknown printer took a galley of type and scrambled it to make a type specimen book."
49514951
----
49524952

4953+
== Enumerable
4954+
4955+
=== lazy [[lazy]]
4956+
4957+
Use `Enumerable#lazy` in combination with methods like `.first` or `.take`, especially for operations on large collections or datasets, because it avoids the need to process the entire collection.
4958+
4959+
[source,ruby]
4960+
----
4961+
size = 10
4962+
4963+
# bad
4964+
(0..10_000_000).uniq { |x| x + 2 }.first(size)
4965+
(0..10_000_000).map { |x| x + 2 }.first(size)
4966+
(0..10_000_000).select { |x| x + 2 }.take(size)
4967+
4968+
# good
4969+
(0..10_000_000).lazy.uniq { |x| x + 2 }.first(size)
4970+
(0..10_000_000).lazy.map { |x| x + 2 }.first(size)
4971+
(0..10_000_000).lazy.select { |x| x + 2 }.take(size)
4972+
----
4973+
49534974
== Heredocs
49544975

49554976
=== Squiggly Heredocs [[squiggly-heredocs]]

0 commit comments

Comments
 (0)