RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
我的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 # (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"

当前题目:我的ruby学习笔记
标题来源:http://sczitong.cn/article/pcgoic.html