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
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
LLM Observabilityによる 対話型音声AIアプリケーションの安定運用
gekko0114
2
430
「ブロックテーマでは再現できない」は本当か?
inc2734
0
930
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
300
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
260
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
680
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
200
フロントエンド開発の勘所 -複数事業を経験して見えた判断軸の違い-
heimusu
7
2.8k
フルサイクルエンジニアリングをAI Agentで全自動化したい 〜構想と現在地〜
kamina_zzz
0
400
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
1k
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
600
20260127_試行錯誤の結晶を1冊に。著者が解説 先輩データサイエンティストからの指南書 / author's_commentary_ds_instructions_guide
nash_efp
1
940
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.3k
Featured
See All Featured
Balancing Empowerment & Direction
lara
5
880
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.6k
Between Models and Reality
mayunak
1
180
The untapped power of vector embeddings
frankvandijk
1
1.6k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
49
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
170
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
380
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
98
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
450
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.5k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
430
A Tale of Four Properties
chriscoyier
162
24k
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?