Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Fun with Enumerator
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
cyclotron3k
August 14, 2018
Programming
1
19
Fun with Enumerator
Fun with Ruby's Enumerator class
cyclotron3k
August 14, 2018
Tweet
Share
More Decks by cyclotron3k
See All by cyclotron3k
Managing concurrent workloads in Ruby
cyclotron3k
0
29
Other Decks in Programming
See All in Programming
CSC307 Lecture 06
javiergs
PRO
0
680
コマンドとリード間の連携に対する脅威分析フレームワーク
pandayumi
1
450
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6k
CSC307 Lecture 04
javiergs
PRO
0
660
dchart: charts from deck markup
ajstarks
3
990
Kotlin Multiplatform Meetup - Compose Multiplatform 외부 의존성 아키텍처 설계부터 운영까지
wisemuji
0
190
humanlayerのブログから学ぶ、良いCLAUDE.mdの書き方
tsukamoto1783
0
190
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
Fluid Templating in TYPO3 14
s2b
0
130
[KNOTS 2026登壇資料]AIで拡張‧交差する プロダクト開発のプロセス および携わるメンバーの役割
hisatake
0
250
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
520
カスタマーサクセス業務を変革したヘルススコアの実現と学び
_hummer0724
0
660
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
Fireside Chat
paigeccino
41
3.8k
[SF Ruby Conf 2025] Rails X
palkan
1
740
Done Done
chrislema
186
16k
The SEO identity crisis: Don't let AI make you average
varn
0
66
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
450
ラッコキーワード サービス紹介資料
rakko
1
2.2M
The Art of Programming - Codeland 2020
erikaheidi
57
14k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
63
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
110
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
690
Transcript
Fun* with Enumerator (*may not be fun)
Enumerator != Enumerable [1, 2, 3].each => #<Enumerator: [1, 2,
3]:each> [1, 2, 3].map => #<Enumerator: [1, 2, 3]:map> [1, 2, 3].select => #<Enumerator: [1, 2, 3]:select>
It gets weird enum = [1, 2, 3].map => #<Enumerator:
[1, 2, 3]:map> enum.each { |n| "<#{n}>" } => ["<1>", "<2>", "<3>"]
None
We can make our own Enumerators enum = Enumerator.new do
|yielder| yielder.yield 1 yielder.yield 2 end enum.to_a => [1, 2]
enum = Enumerator.new do |yielder| puts "Beginning" yielder.yield 1 puts
"Middle" yielder.yield 2 puts "End" end enum.next Beginning => 1 enum.next Middle => 2 enum.next End StopIteration: iteration reached an end
Infinite sequences! fib = Enumerator.new do |yielder| a = 0
b = 1 loop do yielder.yield b tmp = a a = b b = tmp + b end end fib.first 10 => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
Extending Enumerable module Enumerable def my_with_index Enumerator.new do |yielder| i
= 0 self.each do |e| yielder.yield e, i i += 1 end end end end [:foo, :bar, :baz].my_with_index do |e, i| puts "#{i}: #{e}" end.to_a => [[:foo, 0], [:bar, 1], [:baz, 2]]
Miscellany • Use .lazy when working with infinite series •
Useful for managing expensive operations • Does not implement a cache (See CachingEnumerator gem)
Questions?