Ruby on Rails-静态方法 [英] Ruby on rails - Static method

查看:91
本文介绍了Ruby on Rails-静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每5分钟执行一次方法,我在每次使用ruby(cron)时都会实现.但这是行不通的.我认为我的方法无法访问. 我要执行的方法位于一个类中.我认为我必须将该方法设为静态,以便可以使用MyClass.MyMethod访问它.但是我找不到正确的语法,或者我找错了地方.

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

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

推荐答案

要声明静态方法,请写...

To declare a static method, write ...

def self.checkPings
  # A static method
end

...或...

class Myclass extend self

  def checkPings
    # Its static method
  end

end

这篇关于Ruby on Rails-静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆