Ruby on rails - Static method Ruby on rails - Static method ruby ruby

Ruby on rails - Static method


To declare a static method, write ...

def self.checkPings  # A static methodend

... or ...

class Myclass extend self  def checkPings    # Its static method  endend


You can use static methods in Ruby like this:

class MyModel    def self.do_something        puts "this is a static method"    endendMyModel.do_something  # => "this is a static method"MyModel::do_something # => "this is a static method"

Also notice that you're using a wrong naming convention for your method. It should be check_pings instead, but this does not affect if your code works or not, it's just the ruby-style.


Change your code from

class MyModel  def checkPings  endend

to

class MyModel  def self.checkPings  endend

Note there is self added to the method name.

def checkPings is an instance method for the class MyModel whereas def self.checkPings is a class method.