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.3k
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
命名をリントする
chiroruxx
1
380
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
Zoneless Testing
rainerhahnekamp
0
120
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
650
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
720
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
240
talk-with-local-llm-with-web-streams-api
kbaba1001
0
170
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
3
960
Jakarta EE meets AI
ivargrimstad
0
230
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
650
Featured
See All Featured
A better future with KSS
kneath
238
17k
We Have a Design System, Now What?
morganepeng
51
7.3k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Making Projects Easy
brettharned
116
5.9k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
510
Thoughts on Productivity
jonyablonski
67
4.4k
GitHub's CSS Performance
jonrohan
1030
460k
Visualization
eitanlees
146
15k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
What's in a price? How to price your products and services
michaelherold
243
12k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
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Λਪ