Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中为单例类定义initialize有意义吗?_Ruby_Class_Internals - Fatal编程技术网

在Ruby中为单例类定义initialize有意义吗?

在Ruby中为单例类定义initialize有意义吗?,ruby,class,internals,Ruby,Class,Internals,我预计我不会在这里做任何实际的事情,只是试图理解一些更深层次的Ruby概念 如果我有以下代码 class Bookshelf @book_qty = 100 # class instance var class << self attr_accessor :books_qty end def initialize @book = "This book is in every object as an object instance variabl

我预计我不会在这里做任何实际的事情,只是试图理解一些更深层次的Ruby概念

如果我有以下代码

class Bookshelf
  @book_qty = 100 # class instance var

  class << self
    attr_accessor :books_qty 
  end

  def initialize
    @book = "This book is in every object as an object instance variable"
  end

  # so far so good, but what happens with...

  def self.initialize   # what would be this called on ?
    puts " and at what step would this be printed, if ever?"
    # I thought it would be printed when the class code is parsed first time,
    # but no
  end

  # or also

  class << self
    def initialize
      puts "same here"
    end
  end

end
class书架
@账簿数量=100#类实例变量

类为单例类定义
初始化
没有任何意义(在某些情况下,无论您是使用
def self.
还是
class,定义自定义构造函数都是有意义的,但我不会调用方法
initialize
,因为您重写以自定义初始化的实例方法也被称为
initialize
。这会有点混乱

def self.initialize#这将调用什么

如果定义这样的方法,可以通过将该方法直接发送到类来调用它:

Bookshelf.initialize

同样的事情也适用于
类中定义的方法等待一秒钟,但这不是
def initialize(quantity)吗…end
仅在使用Bookshelf.new创建Bookshelf类的对象实例时调用?是的,
Bookshelf.new
对其实例调用
#initialize
。因此,如果您从自己的构造函数调用
.new
。在上面的示例中使用_quantity
),您也会实现同样的效果。Yes=)在第一个示例中,我没有更新类实例变量(这是一个常见的实例变量),但第二个示例描述了如何更新类1。继续发现!=)这个答案与问题无关。@jj_uu是的,您也可以编写
@book\u qty=100
。我个人更喜欢使用setter,因为您可以覆盖它,等等。但是对于上面的示例,这并不重要@马克斯:是的,你是对的。一开始我不理解这个问题,但我会留下它,因为@jj_u发现它很有用。
class Bookshelf
  puts "class defined!"
end
Bookshelf.initialize
class Bookshelf
  attr_accessor :book_qty

  def self.with_quantity(quantity)
    new(quantity)
  end

  def initialize(quantity)
    self.book_qty = quantity
  end
end
bs = Bookshelf.with_quantity 100
bs.quantity # => 100
class Bookshelf
  @book_qty = 1000
  class << self
    attr_accessor :book_qty
  end
end

Bookshelf.book_qty # => 1000
Bookshelf.book_qty = 2000
Bookshelf.book_qty # => 2000