Ruby on rails - Static method

RubyOopStatic Methods

Ruby Problem Overview


I want a method to be executed every 5 minutes, I implemented whenever for ruby (cron). But it does not work. I think my method is not accessible. The method I want to execute is located in a class. I think I have to make that method static so I can access it with MyClass.MyMethod. But I can not find the right syntax or maybe I am looking in the wrong place.

Schedule.rb

every 5.minutes do
  runner "Ping.checkPings"
end

Ping.rb

def checkPings      
  gate =  Net::Ping::External.new("10.10.1.1")
  @monitor_ping = Ping.new()
  
  if gate.ping?        
    MonitorPing.WAN = true
  else 
    MonitorPing.WAN = false
  end
  
  @monitor_ping.save      
end

Ruby Solutions


Solution 1 - Ruby

To declare a static method, write ...

def self.checkPings
  # A static method
end

... or ...

class Myclass extend self

  def checkPings
    # Its static method
  end

end

Solution 2 - Ruby

You can use static methods in Ruby like this:

class MyModel
    def self.do_something
        puts "this is a static method"
    end
end
MyModel.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.

Solution 3 - Ruby

Change your code from

class MyModel
  def checkPings
  end
end

to

class MyModel
  def self.checkPings
  end
end

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.

Solution 4 - Ruby

Instead of extending self for the whole class, you can create a block that extends from self and define your static methods inside.

you would do something like this :

class << self
#define static methods here
end

So in your example, you would do something like this :

class Ping
  class << self
    def checkPings
      #do you ping code here
      # checkPings is a static method
    end
  end
end

and you can call it as follows : Ping.checkPings

Solution 5 - Ruby

There are some ways to declare a static method in RoR.

#1

class YourClassName
  class << self
    def your_static_method (params)
      # Your code here
    end
  end
end

#2

class YourClassName
  def self.your_status_method
    // Your code here
  end
end

Solution 6 - Ruby

You cannot have static methods in Ruby. In Ruby, all methods are dynamic. There is only one kind of method in Ruby: dynamic instance methods.

Really, the term static method is a misnomer anyway. A static method is a method which is not associated with any object and which is not dispatched dynamically (hence "static"), but those two are pretty much the definition of what it means to be a "method". We already have a perfectly good name for this construct: a procedure.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNostrodamusView Question on Stackoverflow
Solution 1 - RubyAshishView Answer on Stackoverflow
Solution 2 - RubySimon WokerView Answer on Stackoverflow
Solution 3 - RubyssriView Answer on Stackoverflow
Solution 4 - RubyJosephView Answer on Stackoverflow
Solution 5 - RubygiapnhView Answer on Stackoverflow
Solution 6 - RubyJörg W MittagView Answer on Stackoverflow