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
65
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
3k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
4.5k
ビューインプレッションの計測方法
to4iki
1
1k
秘伝の `gitconfig`
to4iki
1
400
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
53
Other Decks in Programming
See All in Programming
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
920
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
C++でシェーダを書く
fadis
6
4.1k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
RubyLSPのマルチバイト文字対応
notfounds
0
120
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.5k
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
89
Facilitating Awesome Meetings
lara
50
6.1k
Producing Creativity
orderedlist
PRO
341
39k
Code Reviewing Like a Champion
maltzj
520
39k
Building Applications with DynamoDB
mza
90
6.1k
Ruby is Unlike a Banana
tanoku
97
11k
Become a Pro
speakerdeck
PRO
25
5k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
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.