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
56
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
2.6k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
4.2k
ビューインプレッションの計測方法
to4iki
0
900
秘伝の `gitconfig`
to4iki
1
400
Abema iOS Architecture
to4iki
12
3.1k
timetable-bot
to4iki
0
14k
BLoC Pattern Introduction with Swift
to4iki
2
1.1k
nel
to4iki
0
120
[iOS] ビデオチームのスモールスクラム
to4iki
0
48
Other Decks in Programming
See All in Programming
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
[技育CAMPアカデミア]アイディアを形に!【超入門】スマホアプリ開発〜リリースまでの流れをご紹介
teamlab
PRO
0
360
OpenAPIを中心に考えるAPI開発入門 / Introduction to API Development with a Focus on OpenAPI
seike460
PRO
2
160
コーンフレークから始める モデリング会話入門
ogurotakayuki
0
350
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
0
180
TYPO3 v13 – The road to LTS: What's new and new APIs
luisasofie_xoxo
0
190
"config" ってなんだ? / What is "config"?
okashoi
0
240
HUIT新歓2024「競技プログラミング、やってみませんか?」
slephy2784
1
270
Anthropic Cookbook のおすすめレシピ
schroneko
7
840
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
250
Ruby GitHub Packages
bkuhlmann
0
630
Goのエラースタックトレースの歴史と今後
sonatard
7
1k
Featured
See All Featured
Atom: Resistance is Futile
akmur
259
25k
Code Review Best Practice
trishagee
55
15k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
21
1.6k
The Cult of Friendly URLs
andyhume
74
5.7k
RailsConf 2023
tenderlove
3
540
Fontdeck: Realign not Redesign
paulrobertlloyd
76
4.9k
Adopting Sorbet at Scale
ufuk
68
8.6k
Optimising Largest Contentful Paint
csswizardry
8
2.3k
GitHub's CSS Performance
jonrohan
1025
450k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
30
6k
A Modern Web Designer's Workflow
chriscoyier
689
190k
Debugging Ruby Performance
tmm1
70
11k
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.