Ruby class 运行 方法

Ruby class running methods

我有一个 Ruby Class,其方法基本上 运行 是同一个 Class 中的其他方法列表。例如:

def tidy
    delete_gallery_items
    delete_collections
    import_collections
    assign_albums_to_collections
    reorder_albums
    rebuild_gallery_items
    reposition_collections
end

这些都是运行一个接一个吗,还是会出现后一个在前一个完成之前开始的情况?

一个接一个被调用。后面的一个在轮到之前是不可能被调用的。

我只能想到这样的边缘情况,当这些方法在内部启动新线程时,后面的方法可能比前面的方法更快地处理它的线程。但这并没有改变一个事实,即这些方法将一个接一个地被调用。

好吧,你确实问了。

class C
  def a
    self.class.alias_method :b_old, :b
    self.class.alias_method :c_old, :c
    self.class.alias_method :b, :c_old
    self.class.alias_method :c, :b_old
    puts "a"
  end

  def b
    puts "b"
  end

  def c
    puts "c"
  end

  def tidy
    a
    b
    c
  end
end
C.new.tidy
  #=> nil

显示:

a
c
b