いわゆるモンキーパッチ〜
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 to_s | |
"murajun1978" | |
end | |
end | |
"Ruby".to_s # => "murajun1978" |
便利だけど、すべての振る舞いを変更してしまうので危険(;^ω^)
では、自分で定義したクラスをネームスペースで囲ってみましょう
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 Murajun | |
class String | |
def to_s | |
"murajun1978" | |
end | |
end | |
end | |
"Ruby".to_s # => "Ruby" | |
Murajun::String.new.to_s # => "murajun1978" |
ネームスペースで囲ったときは、10行目のように使用することができますー
( ̄(エ) ̄)彡☆
0 件のコメント:
コメントを投稿