Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
從 Enumerator 看 Ruby 的迭代器
Search
蒼時弦や
July 27, 2019
Programming
150
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
從 Enumerator 看 Ruby 的迭代器
RubyConf TW 2019
蒼時弦や
July 27, 2019
More Decks by 蒼時弦や
See All by 蒼時弦や
2024 - COSCUP - Clean Architecture in Rails
elct9620
2
240
2023 - RubyConfTW - Rethink Rails Architecture
elct9620
0
300
20230916 - DDDTW - 導入 Domain-Driven Design 的最佳時機
elct9620
0
530
2023 - WebConf - 選擇適合你的技能組合
elct9620
0
760
20230322 - Generative AI 小聚 ft. Happy Designer
elct9620
0
510
2022 - 默默會 - 重新學習 MVC 的 Model
elct9620
1
540
MOPCON 2022 - 從 Domain-Driven Design 看網站開發框架隱藏
elct9620
1
550
2022 - COSCUP - 我想慢慢寫程式該怎麼辦?
elct9620
0
330
2022 - COSCUP - 打造高速 Ruby 專案開發流程
elct9620
0
380
Other Decks in Programming
See All in Programming
Jetpack Compose Mechanisms
skydoves
2
730
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
580
すこし踏み込む CancellationToken
htkym
2
1.7k
Simple Storage Service(S3) is not simple
iwatsukayura
0
270
mrbgem 三角測量 開発
ogom
0
240
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
410
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
Event is what happens
moro
1
290
外から家のPCの電源入れたい!をPHPで解決する
kawashimo0
1
130
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
150
コーディング自動化の次へ。人の介入をゼロにするファクトリーとサポートAIによる実践
timakin
0
1.4k
Workers Cache を知る
syumai
0
350
Featured
See All Featured
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.7k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
880
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Heart Work Chapter 1 - Part 1
lfama
PRO
10
37k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
310
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
Making Projects Easy
brettharned
120
6.8k
Product Roadmaps are Hard
iamctodd
55
13k
Navigating Weather and Climate Data
rabernat
0
550
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Transcript
Review Ruby s Iterator with Enumerator Photo by Tine Ivanič
on Unsplash
WEB DEVELOPER GAME DEVELOPER ࣌ ݭ @elct9620
None
As a Ruby developer, We use #each every day
BUT How it works?
#iterator Photo by Joel Fulgencio on Unsplash
def iterator(&block) yield 1 yield 2 yield 3 end
VALUE rb_block_call(VALUE obj, ID mid, int argc, const VALUE *
argv, VALUE (*bl_proc) (ANYARGS), VALUE data2) { struct iter_method_arg arg; arg.obj = obj; arg.mid = mid; arg.argc = argc; arg.argv = argv; return rb_iterate(iterate_method, (VALUE)&arg, bl_proc, data2); } The block call is iterate in ruby
def each(&block) @i = 0 yield @i += 1 until
@i >= 10 end
#loop vs #while
static VALUE loop_i(void) { for (;;) { rb_yield_0(0, 0); }
return Qnil; } Loop is a method with a block
#enumerator Photo by Glenn Carstens-Peters on Unsplash
[].each # => #<Enumerator: []:each>
Why we need Enumerator?
enum = [1, 2, 3].to_enum enum.next # => 1 enum.next
# => 2 enum.next # => 3
Enumerator vs Enumerable
class Backpack include Enumerable def initialize(items) @items = items end
def each(&block) @items.each(&block) end end backpack = Backpack.new([:water, :apple]) backpack.map {}
#generator Photo by m0851 on Unsplash
#to_enum vs Enumerator.new
class List def each(&block) #... end end List.new.to_enum # =>
#<Enumerator: #<List:0x00007fa490988a78>:each>
class List def pop(&block) #... end end List.new.to_enum(:pop) # =>
#<Enumerator: #<List:0x00007fa491081fa0>:pop>
If Enumerator.new didn t have target ruby will create a
Generator
Enumerator.new do |yielder| yielder << 1 yielder << 2 end
Why we need Yielder?
enum = Enumerator.new do yield 1 yield 2 end puts
enum.to_a # => no block given (yield) (LocalJumpError)
class Yielder def initialize(&block) @proc = block.to_proc end def <<(value)
@proc.call(value) self end end
class Generator def initialize(&block) @proc = block.to_proc end def each(&_block)
yielder = Yielder.new { |x| yield x } @proc.call(yielder) end end
#lazy Photo by Kate Stone Matheson on Unsplash
It is hard to figure out it, but useful
class Backpack def each(&block) yield p(1) yield p(2) yield p(3)
end end backpack = Backpack.new.to_enum backpack.map(&:rect).take(1).to_a backpack.lazy.map(&:rect).take(1).to_a
class Backpack def each(&block) yield p(1) yield p(2) yield p(3)
end end backpack = Backpack.new.to_enum backpack.map(&:rect).take(1).to_a backpack.lazy.map(&:rect).take(1).to_a
backpack.map(&:rect).take(1).to_a # => 1 # => 2 # => 3
backpack.lazy.map(&:rect).take(1).to_a # => 1
backpack.take(1).to_a # => 1 backpack.lazy.take(1).to_a # => 1
backpack = Backpack.new.to_enum backpack.lazy.reverse_each.take(1).to_a # => 1 # => 2
# => 3
And last, let s discuss implement #lazy in Ruby
Thanks