Skip to content

Instantly share code, notes, and snippets.

@kasei-san
Created July 25, 2014 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasei-san/029658e58b420aaa5d79 to your computer and use it in GitHub Desktop.
Save kasei-san/029658e58b420aaa5d79 to your computer and use it in GitHub Desktop.
引数 klass で指定したクラスだけに対して、ブロックで指定した機能を提供で きるモジュールを定義します。定義した機能は Module#refine を使用せずに直 接 klass に対して変更を行う場合と異なり、限られた範囲のみ有効にできます。 そのため、既存の機能を局所的に修正したい場合などに用いる事ができます。
定義した機能は main.using を実行した場合のみ有効になります。
オリジナルのソースコードを変更することなく、実行時に動的言語(例えばSmalltalk, JavaScript, Objective-C, Ruby, Perl, Python, Groovy, など)のコードを拡張したり、変更したりする方法である
# 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
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
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