Created
July 25, 2014 10:21
-
-
Save kasei-san/029658e58b420aaa5d79 to your computer and use it in GitHub Desktop.
Module#refine ref: http://qiita.com/kasei-san/items/a334508a4296a2e93c97
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
引数 klass で指定したクラスだけに対して、ブロックで指定した機能を提供で きるモジュールを定義します。定義した機能は Module#refine を使用せずに直 接 klass に対して変更を行う場合と異なり、限られた範囲のみ有効にできます。 そのため、既存の機能を局所的に修正したい場合などに用いる事ができます。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
定義した機能は main.using を実行した場合のみ有効になります。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
オリジナルのソースコードを変更することなく、実行時に動的言語(例えばSmalltalk, JavaScript, Objective-C, Ruby, Perl, Python, Groovy, など)のコードを拡張したり、変更したりする方法である |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# module MutableString を using している所だけで、下記モンキーパッチが適用される | |
module MutableString | |
refine String do | |
def surround_quote | |
"'#{self}'" | |
end | |
def gsub_space_to_atmark | |
self.gsub(' ', "@") | |
end | |
end | |
end | |
class TextDecorator | |
def initialize(str) | |
@base_str = str | |
end | |
using MutableString | |
# String を拡張することで、メソッドチェインが実現 | |
def decorate | |
@base_str.surround_quote.gsub_space_to_atmark | |
end | |
end | |
# undefined method `gsub_space_to_atmark' for "abc def":String (NoMethodError) | |
"abc def".gsub_space_to_atmark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def surround_quote | |
"'#{self}'" | |
end | |
def gsub_space_to_atmark | |
self.gsub(' ', "@") | |
end | |
end | |
class TextDecorator | |
# String を拡張することで、メソッドチェインが実現 | |
def decorate | |
@base_str.surround_quote.gsub_space_to_atmark | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TextDecorator | |
def initialize(str) | |
@base_str = str | |
end | |
def decorate | |
gsub_space_to_atmark(surround_quote(@base_str)) | |
end | |
private | |
def surround_quote(str) | |
"'#{str}'" | |
end | |
def gsub_space_to_atmark(str) | |
str.gsub(' ', "@") | |
end | |
end | |
puts TextDecorator.new("abc def").decorate #-> "abc@def" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment