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
Re: Power Assert in Ruby
Search
k_tsj
November 08, 2015
Programming
2
5k
Re: Power Assert in Ruby
http://regional.rubykaigi.org/oedo05/
k_tsj
November 08, 2015
Tweet
Share
More Decks by k_tsj
See All by k_tsj
[RubyConf2019]Pattern matching - New feature in Ruby 2.7
k_tsj
0
2.7k
Pattern matching - New feature in Ruby 2.7
k_tsj
20
100k
Re: Pattern Matching in Ruby
k_tsj
1
2.2k
Power Assert in Ruby
k_tsj
16
9.5k
Pattern Matching in Ruby
k_tsj
18
6.5k
Other Decks in Programming
See All in Programming
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
200
Better Code Design in PHP
afilina
PRO
0
130
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
230
cmp.Or に感動した
otakakot
3
200
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
Quine, Polyglot, 良いコード
qnighy
4
650
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
役立つログに取り組もう
irof
28
9.6k
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.5k
CSC509 Lecture 09
javiergs
PRO
0
140
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
Featured
See All Featured
Fireside Chat
paigeccino
34
3k
Adopting Sorbet at Scale
ufuk
73
9.1k
Optimizing for Happiness
mojombo
376
70k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Being A Developer After 40
akosma
87
590k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
A designer walks into a library…
pauljervisheath
204
24k
GitHub's CSS Performance
jonrohan
1030
460k
What's in a price? How to price your products and services
michaelherold
243
12k
A Philosophy of Restraint
colly
203
16k
Transcript
RE: POWER ASSERT IN RUBY Kazuki Tsujimoto
ࣗݾհ • “VM Bug Hunter” • ॳAsakusa.rb
• Ξαʔγϣϯϝιου/Ϛονϟͷ͍͚ʹ·ͣʹ ςετΛॻ͚ΔΑ͏ʹ͢ΔͨΊpower_assertΛ࡞ͬͨ • assert {expr}͚͍֮ͩ͑ͯΕΑ͍ • 1==3ͷΓ͕දࣔ͞Ε͍ͯͳ͍ͱ͍͏͕͋Δ $ ruby
test_example.rb assert { [0, 1, 2].find {|i| i.odd? } == 3 } | 1 POWER ASSERT IN RUBY
• ࣮ʹ͍ͬͯΔTracePointͷ੍ • ϝιουݺͼग़͠ͷ࠷దԽʹΑͬͯɺΠϕϯτ͕ىಈ ͞Εͳ͘ͳ͍ͬͯΔ • ࣄલʹpower_assertΛrequireͯ͠࠷దԽΛແޮԽ͢Ε Α͍͕ɺ͜Εखؒ $ ruby
-rpoewr_assert test_example.rb assert { [0, 1, 2].find {|i| i.odd? } == 3 } | | | false 1 POWER ASSERT IN RUBY
• akr͞Μ͔Β࠷దԽରͱͳΔϝιουΛʮ͜ ͱ͝ͱ͘࠶ఆٛͯ͠ݩʹͯ͠͠·ʯ͑Α ͍ͷͰͳ͍͔ͱͷࢦఠ • ࣄલrequireΛෆཁʹ͢Δ͜ͱͰ͖Δͷ͔ POWER ASSERT IN RUBY
• 1 == 3ͷ==ϝιουݺͼग़͠ͷΠϕϯτ͕र ͑ͳ͍ trace = TracePoint.new(:return, :c_return)
{|tp| p tp.method_id } trace.enable { 1 == 3 }
ݪҼ • Fixnum#==ͳͲͷجຊతͳϝιουಛघ έʔεѻ͍Ͱ؆ུԽͨ͠ॲཧΛ͍ͯ͠ΔͨΊ if (FIXNUM_2_P(recv, obj) && BASIC_OP_UNREDEFINED_P(BOP_EQ, FIXNUM_REDEFINED_OP_FLAG))
{ return (recv == obj) ? Qtrue : Qfalse; } else if(...) { ... } ... CALL_SIMPLE_METHOD(recv); ಛघέʔε(1) ಛघέʔε(..N) Ұൠέʔεɹ
ରࡦ • ૉͳΞϓϩʔν(࠶ఆٛͰϑϥάΛཱͯΔ) • Fixnum.method_addedΛϢʔβ͕ఆٛͯ͠ ͍Δ߹ͳͲʹӨڹ͕ग़Δ class Fixnum alias eq_orig
== def ==(other) eq_orig(other) end alias == eq_orig end
ରࡦ • ࠾༻ͨ͠Ξϓϩʔν(࠶ఆٛҎ֎ͰϑϥάΛཱͯΔ) • ՄࢹੑΛ໌ࣔతʹઃఆ͢Δ(Ruby 2.0ʙ2.2) • refine͢Δ(Ruby 2.0ʙ2.3dev) class
Fixnum public :== end module M refine Fixnum do def ==; end end end
ଓɾ • C.new == C.newͷ==ϝιουݺͼग़͠ͷΠ ϕϯτ͕र͑ͳ͍ trace = TracePoint.new(:return, :c_return)
{|tp| p tp.method_id } class C; end trace.enable { C.new == C.new }
ଓɾݪҼ • ϝιουͷ࣮ମ͕ಛఆͷCؔ(rb_obj_equal) ͩͬͨͱ͖ಛघέʔεѻ͍Ͱ؆ུԽͨ͠ॲ ཧΛ͍ͯ͠ΔͨΊ ... vm_search_method(ci, cc, recv); if
(check_cfunc(cc->me, rb_obj_equal)) { return recv == obj ? Qtrue : Qfalse; } ... CALL_SIMPLE_METHOD(recv);
ଓɾରࡦ • ૉͳΞϓϩʔν • ϝιουΛ࠶ఆٛͯ͠ݩʹ͞ͳ͍ • BasicObject.method_added(ུ) • ϞϯΩʔύον͕ద༻͞Εͨঢ়ଶͰϢʔβίʔυ͕࣮ߦ͞ Εͯ͠·͏
class BasicObject alias eq_orig == def ==(other); eq_orig(other); end alias == eq_orig end
ଓɾରࡦ • ࠾༻ͨ͠Ξϓϩʔν • refine͢Δ(refine͞Ε͍ͯΔ͜ͱΛࣔ͢ಛघͳϝιου ΤϯτϦΛrb_obj_equalͷલʹૠೖ͢Δ) • ͲͪΒͷΞϓϩʔνͰRubyͷඪ४Ϋϥεʹ͍ͭͯࣄલʹ ରԠ͓ͯ͘͜͠ͱ͕Մೳ͕ͩɺC֦ுͰ࡞ͨ͠Ϣʔβఆٛ Ϋϥε·Ͱٹ͑ͳ͍ͱ͍͏՝Δ
module M refine BasicObject do def ==; end end end
ଓʑɾ • C.new == C.newͷ==ϝιουݺͼग़͠ͷΠ ϕϯτ͕1ճ͔͠र͑ͳ͍ trace = TracePoint.new(:return, :c_return)
{|tp| p tp.method_id } class C; end trace.enable { loop { C.new == C.new } }
ଓʑɾݪҼ • 1ճͷݺͼग़࣌͠ʹʮ࠷ऴతʹrb_obj_equalΛݺͿʯ ͱ͍͏ใΛΠϯϥΠϯΩϟογϡʹ࣋ͬͯ͠·͏ͨΊ Lightweight Method Dispatch on MRI(Koichi Sasada)͔ΒҾ༻
ଓʑɾରࡦ • దٓΩϟογϡΛΫϦΞ͢ΕΑ͍ • power_assertͰassertʹ͞ΕͨϒϩοΫΛݺ ͼग़͢λΠϛϯάͰΫϦΞ • ΩϟογϡͷΫϦΞʹRefinementsΛར༻ • ۭϞδϡʔϧΛusing
·ͱΊ • power_assertͷࣄલrequire΄΅ෆཁʹ • શͰͳ͍͕௨ৗͷϢʔεέʔεΧόʔ • Rubyͷ෦࣮ʹڧ͘ґଘ͓ͯ͠Γࠓޙಈ࡞ ͢Δ͔ෆಁ໌ • Ҿ͖ଓ͖ࣄલrequireΛਪ