开发者

How to call methods outside of a class

开发者 https://www.devze.com 2023-03-11 12:16 出处:网络
How would I call myfunc and myotherfunc below outside of the class? class Accounting::Invoice < ActiveRecord::Base

How would I call myfunc and myotherfunc below outside of the class?

class Accounting::Invoice < ActiveRecord::Base
  def myfunc
    return true
  end

  class << s开发者_StackOverflow社区elf
    def myotherfunc
      return false
    end
  end
end


myfunc is an instance method, so you first need an instance and then you can call the function:

invoice = Accounting::Invoice.new
invoice.myfunc

myotherfunc is class method, so you just call it on directly on the class object:

Accounting::Invoice.myotherfunc

By the way, this answer is not specific to Rails; it applies to any Ruby program.

This post may be helpful (I didn't read the whole thing): http://railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/

0

精彩评论

暂无评论...
验证码 换一张
取 消