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
ベクトル検索のフィルタを用いた機械学習モデルとの統合 / python-meetup-fukuoka-06-vector-attr
monochromegane
2
390
LangChain4jとは一味違うLangChain4j-CDI
kazumura
1
170
AIコードレビューの導入・運用と AI駆動開発における「AI4QA」の取り組みについて
hagevvashi
0
430
「抽象に依存せよ」が分からなかった新卒1年目の私が Goのインターフェースと和解するまで
kurogenki
0
110
米国のサイバーセキュリティタイムラインと見る Goの暗号パッケージの進化
tomtwinkle
2
550
RAGでハマりがちな"Excelの罠"を、データの構造化で突破する
harumiweb
9
2.8k
野球解説AI Agentを開発してみた - 2026/02/27 LayerX社内LT会資料
shinyorke
PRO
0
120
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
200
AHC061解説
shun_pi
0
360
AWS×クラウドネイティブソフトウェア設計 / AWS x Cloud-Native Software Design
nrslib
15
3k
クライアントワークでSREをするということ。あるいは事業会社におけるSREと同じこと・違うこと
nnaka2992
1
330
PostgreSQL を使った快適な go test 環境を求めて
otakakot
0
540
Featured
See All Featured
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
690
Six Lessons from altMBA
skipperchong
29
4.2k
The agentic SEO stack - context over prompts
schlessera
0
690
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.1k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
240
So, you think you're a good person
axbom
PRO
2
2k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.1k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
82
The Language of Interfaces
destraynor
162
26k
The World Runs on Bad Software
bkeepers
PRO
72
12k
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?