Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Rubyの黒魔術
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
菊本 久寿
September 21, 2024
Programming
280
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rubyの黒魔術
RUNTEQ内のイベントで発表した初学者にはほぼ役に立たないけど、知っておくと面白いメソッドをご紹介してます。
菊本 久寿
September 21, 2024
More Decks by 菊本 久寿
See All by 菊本 久寿
読まないコードリーディング術
hisaju
2
330
モメンタムマーケティング
hisaju
1
250
人生を救ってくれたエンジニアという生き方
hisaju
1
2.7k
ワークシフト
hisaju
0
360
サラリーマンのススメ
hisaju
0
2.1k
つくらないキャリアの作り方
hisaju
0
1.7k
キャリアにつなげる副業のやり方
hisaju
2
2.1k
若手や未経験者を育てるときのメンターの心構え
hisaju
0
640
Other Decks in Programming
See All in Programming
型解析で実現する Go の言語内 DSL / Conference に Go! タイムテーブルの歩き方 for Gophers
mazrean
0
140
標準パッケージに uuid が追加された 背景から見る Go らしい意思決定 / go_127_uuid_decision
convto
5
6.1k
Building an Out-of-Order CPU
latte72
1
750
新卒PdEのリアル
ryu1013
1
490
AI × TiDD / 2026.09.05 Redmine 大阪
tokudiro
1
120
Kiroで創り、AgentCoreで繋ぐ!AWSで実践する「AI-DLC」から「AIエージェント統合」までの最新地図
licux
4
650
Deep dive into the select statement (GopherCon UK)
jespino
0
170
DynamoDBの基礎を振り返りながらベクトル検索機能を理解する
musan
3
280
「AI時代、配布するPythonコードをどう守るか: 難読化の実験と判断軸」 #PyconJP2026
pkshadeck
PRO
2
200
GKE アップグレード前に知っておきたい Blue/Green と PDB の関係
stkk
0
160
Swift愛好会と私(ウホーイ) / Swift Fan Club and Uhooi
uhooi
0
150
Security issues being discussed on Web Platforms
petamoriken
0
740
Featured
See All Featured
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
300
A Modern Web Designer's Workflow
chriscoyier
699
190k
Test your architecture with Archunit
thirion
2
2.4k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
Visualization
eitanlees
152
17k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
450
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
410
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
440
AI: The stuff that nobody shows you
jnunemaker
PRO
9
990
Transcript
推しメソッド&Gem LT 会 Rubyの黒魔術
自己紹介 HN:ひさじゅ Ruby歴: 13年くらい 好きな書き方:メタプロ 好きなRubyの機能:mixin 得意技:モンキーパッチ、concern 座右の銘:可読性よりワンライナー 2
Rubyが 黒魔術と 呼ばれる理由 3
Rubyが黒魔術と呼ばれる理由 定義されたメソッドが見当たらない 4
とりあえずenumのソースを読んでみる 5 すべてを改造可能にする 「module_eval」 ブロックをあたかもクラス 定義やモジュール定義の中 のコードであるかのように 実行する ※ class_evalと同じ
module_evalでGemを魔改造 6 Faker::JapaneseMedia::Conan これだけじゃ足りない 映画タイトルも欲しいニダ
module_evalでGemを魔改造 7 コナンの映画タイトルを 取得できるように 改造するニダ
module_evalでGemを魔改造 8 config/initializers/faker_extend.rb 実験するニダ config/locale/faker_ja.yml rails c
Fakerのソースも読んでみる 9 無限にメソッドを増殖 「method_missing」 メソッドが見つからなかった時 に呼ばれる。method_missing を用いて呼ばれるメソッドを 「ゴーストメソッド」と呼ぶ。
method_missingで無限ゴーストメソッド 10 method_missing.rb kaiouken_◦◦bai を全部ゴーストメソッド化 するニダ
method_missingで無限ゴーストメソッド 11 成功ニダ
method_missingで無限ゴーストメソッド 12 method_missing.rb すべてをゴーストメソッド化 するニダ
method_missingで無限ゴーストメソッド 13 これで何のメソッドを 呼んでも エラーは出ないニダ
sendで文字からメソッドを実行 14 文字列からメソッド呼び出し 「send」 引数に指定された文字列をメソ ッドとして実行。2つ目以降の引 数は対象メソッドの引数に 合成した文字列から メソッドを実行するニダ
まとめ module_eval • Gemやライブラリを魔改造出来る悪魔のメソッド method_missing • メソッドが存在しないエラーを無効化し、ゴーストメソッドを量産 send • 動的に合成した文字列からメソッドを呼び出せる黒魔術メソッド
15 他の言語出身のエンジニアから 嫌われること間違いなしニダ