我的ruby學習筆記

1.Moudle的方法
 undef_method(),會刪除所以的方法,包括繼承來的的方法。

 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"


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章