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
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
550
AI によるインシデント初動調査の自動化を行う AI インシデントコマンダーを作った話
azukiazusa1
1
720
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
180
2026年 エンジニアリング自己学習法
yumechi
0
130
CSC307 Lecture 09
javiergs
PRO
1
830
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
230
360° Signals in Angular: Signal Forms with SignalStore & Resources @ngLondon 01/2026
manfredsteyer
PRO
0
120
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
190
Smart Handoff/Pickup ガイド - Claude Code セッション管理
yukiigarashi
0
130
インターン生でもAuth0で認証基盤刷新が出来るのか
taku271
0
190
CSC307 Lecture 01
javiergs
PRO
0
690
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
230
Featured
See All Featured
Utilizing Notion as your number one productivity tool
mfonobong
3
220
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.4k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
300
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
80
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
450
Color Theory Basics | Prateek | Gurzu
gurzu
0
200
Typedesign – Prime Four
hannesfritz
42
2.9k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.3k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.3k
We Are The Robots
honzajavorek
0
160
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?