简体   繁体   English

在Ruby 2.0中,如何对特征使用mix方法

[英]In Ruby 2.0 how do you use the mix method for traits

module A

end

class D

  mix A

end  

c1 = D.new

Here is my output: 这是我的输出:

archie@local$ruby mixins-traits.rb 
mixins-traits.rb:4:in `<class:D>': undefined method `mix' for D:Class (NoMethodError)
    from mixins-traits.rb:3:in `<main>'

Traits didn't make it into Ruby 2.0. 特性未将其纳入Ruby 2.0。 They are neither mentioned in the Release Notes nor in the NEWS . 发行说明新闻中均未提及它们。

I don't know ruby has mix keyword? 我不知道红宝石有混合关键字吗? usually it use include keyword. 通常它使用include关键字。 example: class D include A end . 例如: class D include A end

Yes, you have to add an include, for example: 是的,您必须添加一个包含,例如:

module A
  def a1
  end
end

class Sample  
  include A    #include method
end

samp = Sample.new

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM