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
13
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
26
Other Decks in Programming
See All in Programming
ソフトウェアテスト徹底指南書の紹介
goyoki
1
150
AWS発のAIエディタKiroを使ってみた
iriikeita
1
180
ファインディ株式会社におけるMCP活用とサービス開発
starfish719
0
290
Testing Trophyは叫ばない
toms74209200
0
850
プロポーザル駆動学習 / Proposal-Driven Learning
mackey0225
2
1.2k
もうちょっといいRubyプロファイラを作りたい (2025)
osyoyu
0
400
AI Coding Agentのセキュリティリスク:PRの自己承認とメルカリの対策
s3h
0
200
How Android Uses Data Structures Behind The Scenes
l2hyunwoo
0
400
基礎から学ぶ大画面対応(Learning Large-Screen Support from the Ground Up)
tomoya0x00
0
410
「待たせ上手」なスケルトンスクリーン、 そのUXの裏側
teamlab
PRO
0
480
Updates on MLS on Ruby (and maybe more)
sylph01
1
180
複雑なドメインに挑む.pdf
yukisakai1225
5
1.1k
Featured
See All Featured
A designer walks into a library…
pauljervisheath
207
24k
For a Future-Friendly Web
brad_frost
180
9.9k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
61k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
187
55k
Bash Introduction
62gerente
615
210k
4 Signs Your Business is Dying
shpigford
184
22k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1.1k
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
Designing for Performance
lara
610
69k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
358
30k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
Designing for humans not robots
tammielis
253
25k
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?