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
22
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Fun with Enumerator
Fun with Ruby's Enumerator class
cyclotron3k
August 14, 2018
More Decks by cyclotron3k
See All by cyclotron3k
Managing concurrent workloads in Ruby
cyclotron3k
0
33
Other Decks in Programming
See All in Programming
新卒PdEのリアル
ryu1013
1
340
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
0
170
in-process GraphQL のすすめ #ginzajs
izumin5210
4
1.5k
Building an Out-of-Order CPU
latte72
0
650
Swift愛好会と私(ウホーイ) / Swift Fan Club and Uhooi
uhooi
0
120
私のClaude Code活用法 (個人開発編) - PHPerKaigi mini #4(2026/08/24)
panda_program
1
210
高専、大学編入、そして未踏へ〜プロダクト開発とキャリアの歩み - Technical College, University Transfer, and On to “Mitou” / My Journey in Product Development and Career
pkmiya
0
130
FastAPI の並行処理モデルを完全に理解する
hoto17296
9
3.7k
Go 1.27からのGODEBUG / Go 1.27 リリースパーティ #go127party
mazrean
0
300
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
0
130
初めての模倣学習とVLA
natsutan
0
460
XP祭りでしか伝わらないフリップネタ #xpjug
murabayashi
0
110
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
GitHub's CSS Performance
jonrohan
1033
470k
How to Talk to Developers About Accessibility
jct
2
540
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
A designer walks into a library…
pauljervisheath
211
25k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
360
Mind Mapping
helmedeiros
PRO
1
340
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
450
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
520
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.9k
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?