Something about Instance Method and Class Method

转载 2011年01月12日 11:14:00
If you wanted to define several methods for
class Array, you'd write

class Array
  [define methods here]
end

and all instances of Array can now use your methods

similarly, if you want to define several methods for an object's singleton
class, you write

class << object
  [define methods here]
end

and all instances of object's singleton class (i.e., just object) can
now access the methods.

As for class methods - well, a class is just an instance of class Class.
Therefore, it has methods which are instance methods of Class. You can
add in methods by saying

class Class
  [define methods here]
end

and all classes will now have the new class methods. If you want class
methods for a single class, you are actually tacking on methods to an
object, in much the same way as "def a.newmethod" would. So you can use
that syntax:

def Array.newmethod1
  [define]
end

or you can say

class << Array
  [define]
end

or, if you are already *in* class Array and defining instance methods,
you can highilght the fact that you're working in the same conceptual
space by taking advantage of Ruby's setting 'self' to 'Array' when you say
'class Array', and writing

class Array
  def instancemethod
    [define]
  end

  class << self
    def classmethod
      [define]
    end
  end
end

There's no arcane magic going on here - self is an object of class
Class, and in this case it has the value Array.

Try the following:

class Array
  p self
  p self.class
  p self.id
end

class << Array
  p self
  p self.class
  p self.id
end

class Array
  class << self
    p self
    p self.class
    p self.id
  end
end

a = Array.new
class << a
  p self
  p self.class
  p self.id
end

and, to prove that singleton classes are being created, even though they
all say Array,

b = Array.new
class << b
  p self
  p self.id
end
   
p Array.id

相关文章推荐

Model Class Method Instance Variables

=begin # 这章主要是说下ruby的model 类 方法 实例变量 Ruby语言中,任何的事物都是对象(Object),包含:模块(Model)、类(Class)、方法(Method)、实例...

Java Class and Method Modifiers

  • 2012-03-04 14:07
  • 161KB
  • 下载

Class method differences in Python: bound, unbound and static

原文出处:http://stackoverflow.com/questions/114214/class-method-differences-in-python-bound-unbound-and-...

paper about climbing string method

  • 2014-07-15 16:23
  • 3.40MB
  • 下载

something about Compile and Link on Embedded Linux

1 , Compile 流程 2,The `##' preprocessing operator performs token pasting #define mypr...

[知其然不知其所以然-23] Something about the printk loglevel and var_list

What does the console_loglevel take effect

Something about C pointers and arrays

#include #include void display(int*, int size); void display2DArray(int (*)[5], int nrow); ...

Something about virtual function and pure virtual function

Firstly, let's demonstrate a fact that virtual function must be implemented. class A { public: A...

No know instance method for selector 'respondsToSelector:'

错误提示:No know instance method for selector'respondsToSelector:'之前写代理的时候,都没有去调用respondsToSelector:去检查我...

Another method get instance of JAVA Object

Another method get instance of JAVA Object
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)