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
Content Security Policy入門 セキュリティ設定と 違反レポートのはじめ方 / Introduction to Content Security Policy Getting Started with Security Configuration and Violation Reporting
uskey512
1
520
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
190
Outline View in SwiftUI
1024jp
1
320
as(型アサーション)を書く前にできること
marokanatani
9
2.6k
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
受け取る人から提供する人になるということ
little_rubyist
0
230
subpath importsで始めるモック生活
10tera
0
300
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
110
RubyLSPのマルチバイト文字対応
notfounds
0
120
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
870
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Navigating Team Friction
lara
183
14k
Facilitating Awesome Meetings
lara
50
6.1k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Unsuck your backbone
ammeep
668
57k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Music & Morning Musume
bryan
46
6.2k
A Tale of Four Properties
chriscoyier
156
23k
Statistics for Hackers
jakevdp
796
220k
Being A Developer After 40
akosma
86
590k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
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Λਪ