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
4.9k
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.4k
Other Decks in Programming
See All in Programming
推し活としてのrails new/oshikatsu_ha_iizo
sakahukamaki
3
1.1k
飲食業界向けマルチプロダクトを実現させる開発体制とリアルな現状
hiroya0601
1
380
PLoP 2024: The evolution of the microservice architecture pattern language
cer
PRO
0
1.1k
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
14
3.8k
gopls を改造したら開発生産性が高まった
satorunooshie
8
230
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
1.7k
CSC305 Lecture 13
javiergs
PRO
0
120
Kubernetes for Data Engineers: Building Scalable, Reliable Data Pipelines
sucitw
1
180
go.mod、DockerfileやCI設定に分散しがちなGoのバージョンをまとめて管理する / Go Connect #3
arthur1
10
2.3k
ActiveRecordの力でDBのメタデータを迅速に解析する
lnit
5
2.2k
Importmapを使ったJavaScriptの 読み込みとブラウザアドオンの影響
swamp09
3
1.2k
組織に自動テストを書く文化を根付かせる戦略(2024秋版) / Building Automated Test Culture 2024 Autumn Edition
twada
PRO
10
4.3k
Featured
See All Featured
A Tale of Four Properties
chriscoyier
156
23k
What's in a price? How to price your products and services
michaelherold
243
11k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.6k
Building Applications with DynamoDB
mza
90
6k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
92
16k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
The Art of Programming - Codeland 2020
erikaheidi
51
13k
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
Speed Design
sergeychernyshev
24
560
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
41
9.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
43
6.6k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
225
22k
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Λਪ