私のruby学習ノート
1.Moduleのメソッドundef_method()は、継承されたメソッドを含む既存のメソッドを削除します.
remove_method()は,受信者自身のメソッドのみを削除する.
2、単品方法
単品メソッドとは,特定のオブジェクト特有のメソッドであってもruby中のクラスはオブジェクトであるため,クラスメソッドが単品メソッドである.
例:
これはとても理解しやすくて、ほほほ!
3.Moudle#class_evel()メソッドは、既存のクラスのコンテキストでブロックを実行します.
remove_method()は,受信者自身のメソッドのみを削除する.
2、単品方法
単品メソッドとは,特定のオブジェクト特有のメソッドであってもruby中のクラスはオブジェクトであるため,クラスメソッドが単品メソッドである.
例:
class A
def method_a
"this is a method"
end
end
aa = A.new
bb = A.new
aa.method_a #=>"this is a method"
bb.method_a #=>"this is a method"
def aa.method_b
"this is b method"
end
p aa.method_b #=>"this is b method"
p bb.method_b #=>"undefined method `method_b' for #<A:0x9a242a8> (NoMethodError)"
これはとても理解しやすくて、ほほほ!
3.Moudle#class_evel()メソッドは、既存のクラスのコンテキストでブロックを実行します.
def add_method_to(a_class)
a_class.class_eval do
def m; "hello" ; end
end
end
add_method_to String
"abc".m #=> "hello"