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
to4iki
September 07, 2014
Programming
0
68
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
suspend-view-controller-sample
to4iki
0
3.1k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
4.6k
ビューインプレッションの計測方法
to4iki
1
1k
秘伝の `gitconfig`
to4iki
1
420
Abema iOS Architecture
to4iki
12
3.3k
timetable-bot
to4iki
0
14k
BLoC Pattern Introduction with Swift
to4iki
2
1.2k
nel
to4iki
0
140
[iOS] ビデオチームのスモールスクラム
to4iki
0
60
Other Decks in Programming
See All in Programming
React 19アップデートのために必要なこと
uhyo
8
1.6k
バッチを作らなきゃとなったときに考えること
irof
2
560
GoとPHPのインターフェイスの違い
shimabox
2
220
color-scheme: light dark; を完全に理解する
uhyo
7
510
Lambdaの監視、できてますか?Datadogを用いてLambdaを見守ろう
nealle
2
790
Visual StudioのGitHub Copilotでいろいろやってみる
tomokusaba
1
220
自力でTTSモデルを作った話
zgock999
0
120
Rails 1.0 のコードで学ぶ find_by* と method_missing の仕組み / Learn how find_by_* and method_missing work in Rails 1.0 code
maimux2x
1
260
SwiftUI Viewの責務分離
elmetal
PRO
2
280
Duke on CRaC with Jakarta EE
ivargrimstad
0
180
CDKを使ったPagerDuty連携インフラのテンプレート化
shibuya_shogo
0
120
推しメソッドsource_locationのしくみを探る - はじめてRubyのコードを読んでみた
nobu09
2
360
Featured
See All Featured
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
Statistics for Hackers
jakevdp
797
220k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
28
1.9k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3k
Fontdeck: Realign not Redesign
paulrobertlloyd
83
5.4k
Done Done
chrislema
182
16k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
440
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
GraphQLとの向き合い方2022年版
quramy
44
14k
Building Adaptive Systems
keathley
40
2.4k
Measuring & Analyzing Core Web Vitals
bluesmoon
6
260
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.