How to call methods outside of a class
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/
精彩评论