Last active
August 29, 2015 14:19
-
-
Save quanon/a14b17d12b51e678ad07 to your computer and use it in GitHub Desktop.
Module#refine メモ
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 GoodName | |
refine(String) do | |
def good_name? | |
true | |
end | |
end | |
def bad_name? | |
false | |
end | |
end | |
class Girl | |
using(GoodName) | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
end | |
def good? | |
@name.good_name? | |
end | |
def bad? | |
bad_name? | |
end | |
end | |
girl = Girl.new('Kurisu Makise') | |
#=> #<Girl:0x007fec121dcd00 @name="Kurisu Makise"> | |
girl.good? | |
#=> true | |
# using(GoodName) では GoodName#bad_name? は定義されない。 | |
girl.bad? | |
#=> NoMethodError: undefined method `bad_name?' for #<Girl:0x007fec121dcd00 @name="Kurisu Makise"> | |
# Girl クラス外からは String#good_name? は呼べない。 | |
girl.name.good_name? | |
#=> NoMethodError: undefined method `good_name?' for "Kurisu Makise":String |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment