English 中文(简体)
鲁比克族类方法
原标题:Ruby Class Methods vs. Methods in Eigenclasses

该类的异构体(或元)中的等级方法和方法只有两种方式才能界定一点?

否则,差异是什么?

class X
  # class method
  def self.a
    "a"
  end

  # eigenclass method
  class << self
    def b
      "b"
    end
  end
end

http://code>X.a和X.b 行为方式不同?

我认识到,我可以通过打开头等舱而超越仪式或别的班级方法:

irb(main):031:0> class X; def self.a; "a"; end; end
=> nil
irb(main):032:0> class X; class << self; alias_method :b, :a; end; end
=> #<Class:X>
irb(main):033:0> X.a
=> "a"
irb(main):034:0> X.b
=> "a"
irb(main):035:0> class X; class << self; def a; "c"; end; end; end
=> nil
irb(main):036:0> X.a
=> "c"
最佳回答

这两种方法相同。 异构体版本有助于使用tr-*方法,例如:

class Foo
  @instances = []
  class << self;
    attr_reader :instances
  end
  def initialize
    self.class.instances << self
  end
end

2.times{ Foo.new }
p Foo.instances
#=> [#<Foo:0x2a3f020>, #<Foo:0x2a1a5c0>]

也可以使用<条码>define_singleton_method,在以下类别上制定方法:

Foo.define_singleton_method :bim do "bam!" end
问题回答

确实,在鲁比没有班级方法。 由于所有物体都是Ruby(包括班级)的标语,你说def self.class_method,你只是真正界定了“”类单一吨法。 类别。 回答你的问题,说

class X
  def self.a
    puts "Hi"
  end

  class << self
    def b
      puts "there"
    end
  end
end

X.a # => Hi
X.b # => there

is two ways of saying the same thing. Both these methods are just singeton (eigen, meta, ghost, or whatever you want to call them) methods defined in the instance of your Class object, which in your example was X. This topic is part of metaprogramming, which is a fun topic, that if you have been using Ruby for a while, you should check out. The Pragmatic Programmers have a great book on metaprogramming that you should definitely take a look at if you interested in the topic.

这里还有另一头脑 ne子,以躲避这一老问题...... 您可能不了解的一件事是,将类别方法标记为/ private(使用私人关键词而不是: 私人-阶级_method)不同于对单等方法的标识:

class Foo
  class << self
    def baz
      puts "Eigenclass public method."
    end

    private
    def qux
      puts "Private method on eigenclass."
    end
  end
  private
  def self.bar
    puts "Private class method."
  end
end

Foo.bar
#=> Private class method.
Foo.baz
#=> Eigenclass public method.
Foo.qux
#=> NoMethodError: private method `qux  called for Foo:Class
#   from (irb)

以下例子将说明前一个例子的意图:

class Foo
  class << self
    def baz
      puts "Eigen class public method."
    end

    private
    def qux
      puts "Private method on eigenclass."
    end
  end
  def bar
    puts "Private class method."
  end
  private_class_method :bar
end
Foo.bar
#=> NoMethodError: private method `bar  called for Foo:Class
#     from (irb)
Foo.baz
#=> Eigen class public method.
Foo.qux
#=> NoMethodError: private method `qux  called for Foo:Class
#     from (irb)

Most instance methods used in Ruby are global methods. That means they are available in all instances of the class on which they were defined. In contrast, a singleton method is implemented on a single object.

存在着明显的矛盾。 课程和所有方法都必须与班级挂钩。 单一州方法所定义的物体不是一类(即一类)。 如果只有课堂能够储存方法,那么物体如何储存单一吨方法? 当采用单一吨方法时,Ruby自动创立了一个匿名类别来储存这种方法。 这些匿名课被称为美术,也称为单州级或单体。 单一吨法与甲型六氯环己烷相关,后者与单一吨方法所定义的物体有关。

如果在单一物体内界定多种单吨方法,则所有方法都储存在同一元。

class Zen
end

z1 = Zen.new
z2 = Zen.new

def z1.say_hello  # Notice that the method name is prefixed with the object name
  puts "Hello!"
end

z1.say_hello    # Output: Hello!
z2.say_hello    # Output: NoMethodError: undefined method `say_hello …

在上述例子中,在Zent族Z1级的案例中界定了“hello”方法,而不是“z2”类。

下面的例子表明,对单一州方法的定义有不同,结果相同。

class Zen
end

z1 = Zen.new
z2 = Zen.new

class << z1
  def say_hello
    puts "Hello!"
  end
end

z1.say_hello    # Output: Hello!
z2.say_hello    # Output: NoMethodError: undefined method `say_hello …

在上述例子中,甲型六氯环己烷和乙型六氯环己烷;z1 改变目前的自称水平,以标明甲型六氯环己烷的甲型六氯环己烷;然后界定甲型六氯环己烷的 say。

上述两个例子都说明了单一州的方法如何运作。 然而,界定单一吨方法比较容易:使用一种称为“方法”的内在方法。

class Zen
end

z1 = Zen.new
z2 = Zen.new

z1.define_singleton_method(:say_hello) { puts "Hello!" }

z1.say_hello    # Output: Hello!
z2.say_hello    # Output: NoMethodError: undefined method `say_hello …

我们早些时候了解到,这些班级也属于物体(属于所谓“级”的建筑班)。 我们还了解到阶级方法。 Class methods are no than oneton methods associated with a category Object.

还有一个例子:

class Zabuton
  class << self
    def stuff
      puts "Stuffing zabuton…"
    end
  end
end

所有的物体均可能都有美分。 这意味着,班级也可以有美分。 在上述例子中,阶级和带;以及自食其力的自我修饰,从而标明Zabuton阶级的阶级。 如果在没有明确接收人的情况下界定某种方法(该方法所定义的类别/目标),则该方法在目前范围内即目前自我价值范围内被默示界定。 因此,在Zabuton阶级的子体内界定了 st子方法。 以上例子只是确定一种等级方法的另一个途径。

更多读到





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...