0

I am very new to RoR and I have played around the source code. But I have a problem that I already built a 'def A' for creating first CSV file, and 'def B' for creating second CSV file. Each 'def' has its own button, but I have the third button to create all CSVs (to produce output from first and second CSV files.)

What is the possible way to do it?

def first_csv
...
end

def second_csv
..
end

def all_csv
<< how to call get first and second csv >>
end

Thanks in advance,

4

2 回答 2

2

它应该和你想象的一样简单:

def all_csv
  first_csv
  second_csv
end
于 2013-08-14T07:36:00.190 回答
2

蒙塔西姆的回答是正确的,但我必须添加一些额外的信息。

Ruby 提供了两种方法。类方法和实例方法。

class MyClass < AvtiveRecord::Base

  # class method
  def self.foo
    # do something
    # within this scope the keyword "self" belongs to the class
  end

  # another class method which calls internally the first one
  def self.bar
    something = foo # self.foo could also be written
    # do something with something
    # within this scope the keyword "self" belongs to the class
  end

  # instance method
  def foo
    # do something
    # if you use the keyword "self" within an instance method, it belongs to the instance
  end

  # another instance method which calls class and the first instance method
  def bar
    mystuff = Myclass.bar # if you want to call the class method you cannot use "self", you must directly call the class
    mystuff2 = foo # self.foo is the same, because of the instance scope
    return [mystuff, mystuff2]
  end

end

您可以调用最后一个实例方法,如下所示

instance = MyClass.first
instance.bar
于 2013-08-14T07:58:56.413 回答