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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
31
Other Decks in Programming
See All in Programming
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
14
5.7k
Mujeres en SEO Summit 2026 - Greatest Disaster Hits en Web Performance
guaca
0
190
1B+ /day規模のログを管理する技術
broadleaf
0
100
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
250
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
720
Lessons from Spec-Driven Development
simas
PRO
0
210
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
120
セキュリティの専門家じゃなくてもできる。「セキュリティ意識」をアップデートして サプライチェーン攻撃への耐性を高めよう。
tk3fftk
5
890
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
350k
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.4k
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
280
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
225
10k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
Building the Perfect Custom Keyboard
takai
2
800
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
330
YesSQL, Process and Tooling at Scale
rocio
174
15k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
430
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
1
350
The Cost Of JavaScript in 2023
addyosmani
55
10k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.3k
Evolving SEO for Evolving Search Engines
ryanjones
0
220
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?