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
ruby-refinements
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
to4iki
September 07, 2014
Programming
0
81
ruby-refinements
http://ruby-doc.org/core-2.1.1/doc/syntax/refinements_rdoc.html
to4iki
September 07, 2014
Tweet
Share
More Decks by to4iki
See All by to4iki
Claude Code の活用事例
to4iki
0
150
Swift Concurrencyを利用したUIViewController表示の排他制御の実装
to4iki
0
3.6k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
5.4k
ビューインプレッションの計測方法
to4iki
1
1.1k
秘伝の `gitconfig`
to4iki
1
460
Abema iOS Architecture
to4iki
12
3.5k
timetable-bot
to4iki
0
15k
BLoC Pattern Introduction with Swift
to4iki
2
1.4k
nel
to4iki
0
180
Other Decks in Programming
See All in Programming
AI時代の脳疲弊と向き合う ~言語学としてのPHP~
sakuraikotone
1
1.5k
20260313 - Grafana & Friends Taipei #1 - Kubernetes v1.36 的開發雜記:那些困在 Alpha 加護病房太久的 Metrics
tico88612
0
230
ベクトル検索のフィルタを用いた機械学習モデルとの統合 / python-meetup-fukuoka-06-vector-attr
monochromegane
2
510
ポーリング処理廃止によるイベント駆動アーキテクチャへの移行
seitarof
3
1.2k
Kubernetesでセルフホストが簡単なNewSQLを求めて / Seeking a NewSQL Database That's Simple to Self-Host on Kubernetes
nnaka2992
0
180
How to stabilize UI tests using XCTest
akkeylab
0
140
生成 AI 時代のスナップショットテストってやつを見せてあげますよ(α版)
ojun9
0
300
Understanding Apache Lucene - More than just full-text search
spinscale
0
140
脱 雰囲気実装!AgentCoreを良い感じにWEBアプリケーションに組み込むために
takuyay0ne
3
380
GC言語のWasm化とComponent Modelサポートの実践と課題 - Scalaの場合
tanishiking
0
120
「効かない!」依存性注入(DI)を活用したAPI Platformのエラーハンドリング奮闘記
mkmk884
0
170
AI 開発合宿を通して得た学び
niftycorp
PRO
0
160
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.5k
BBQ
matthewcrist
89
10k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
220
Large-scale JavaScript Application Architecture
addyosmani
515
110k
The Curse of the Amulet
leimatthew05
1
10k
The agentic SEO stack - context over prompts
schlessera
0
710
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.2k
Skip the Path - Find Your Career Trail
mkilby
1
88
Exploring anti-patterns in Rails
aemeredith
2
290
Raft: Consensus for Rubyists
vanstee
141
7.4k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
Transcript
Ruby2.1 Refinements Λ৮ͬͯΈͨ Toshiki Takezawa @to4iki
What is Refinements
refinement: ચ࿅, վળ
“ࢦఆͨ͠moduleͷதͰ͚ͩɺ ಛఆͷclassͷmethod Λࠩ͠ସ͑Δػೳ”
open class
RubyStringͳͲͷbuiltin class ΛؚΊطଘͷclassmethodΛ Ճɾ্ॻ͖͢Δ͜ͱ͕Ͱ͖Δ
class String def hello
"#{self} hello!" end end puts 'hoge'.hello #=> 'hoge hello!' puts 'fuga'.hello #=> 'fuga hello!'
Monkey Patch
open classͰطଘͷmethod Λஔ͖͑Δ͜ͱ
class String def length
5 end define_method :to_i, -‐> { "#{self} convert number" } end puts 'a'.length #=> 5 puts 'javascript'.length #=> 5 puts '0'.to_i #=> '0 convert number' puts 'hoge'.to_i #=> 'hoge convert number'
powerful!! but danger!!
طଘclassΛࢥ͍ͷ··मਖ਼Ͱ͖Δརศੑ ϧʔϧΛഁյͯ͠͠·͏ةݥੑ programશମʹӨڹΛ༩͑ͯ͠·͏ةݥੑ
it is a turn of refinements!
Demo
ex. Ϣʔβ͕࿈ଓͰϩάΠϯ͠ ͍ͯΔ͔Ͳ͏͔ΛΓ͍ͨ࣌
module TimeExtensions refine Time do
def changed_date? self.beginning_of_day < Time.zone.now.beginning_of_day end end end class User < ActiveRecord::Base using TimeExtensions def login! if self.login_at.changed_date? # add login bonus end end end
“Ruby܅Λ৴པ͢Δɻ Ruby܅Λผͷ͋ΔϓϩάϥϚͱͯ͠ѻ͏ɻ RubyϝλϓϩάϥϛϯάͷΑ͏ͳڧྗͳྗΛ༩͑Δɻ ͨͩ͠ɺେ͍ͳΔྗʹɺେ͍ͳΔ͕͏͜ͱΛ Ε͍͚ͯͳ͍” -ʮϝλϓϩάϥϛϯάRubyʯংจΑΓ
Thanks.