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
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
100
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
500
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
940
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
2
110
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
340
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
return文におけるstd::moveについて
onihusube
1
1.2k
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.7k
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Optimising Largest Contentful Paint
csswizardry
33
3k
Building Adaptive Systems
keathley
38
2.3k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
0
98
Building Applications with DynamoDB
mza
91
6.1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
A Philosophy of Restraint
colly
203
16k
Gamification - CAS2011
davidbonilla
80
5.1k
A Tale of Four Properties
chriscoyier
157
23k
How STYLIGHT went responsive
nonsquared
95
5.2k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
Become a Pro
speakerdeck
PRO
26
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Λਪ