检查Internet连接使用Ruby的存在? [英] Check if Internet Connection Exists with Ruby?

查看:132
本文介绍了检查Internet连接使用Ruby的存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚问如何check如果Internet连接存在使用javascript ,并得到了一些伟大的答案。什么是Ruby中做到这一点最简单的方法?在试图使生成的HTML标记code尽可能的干净,我想有条件地渲染取决于与否网络条件下的JavaScript文件的脚本标签。喜欢的东西(这是HAML):

   - 如果internet_connection?
    %脚本{:SRC => http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js:类型=> 文/ JavaScript的}
- 其他
    %脚本{:SRC => /shared/javascripts/jquery/jquery.js:类型=> 文/ JavaScript的}


解决方案

您可以使用类。

 要求'RESOLV替换
需要'平'高清internet_connection?
  Ping.pingechogoogle.com,1,80
结束

该方法返回真正,并不会引发异常。

Just asked how to check if an internet connection exists using javascript and got some great answers. What's the easiest way to do this in Ruby? In trying to make generated html markup code as clean as possible, I'd like to conditionally render the script tag for javascript files depending on whether or not an internet condition. Something like (this is HAML):

- if internet_connection?
    %script{:src => "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", :type => "text/javascript"}
- else
    %script{:src => "/shared/javascripts/jquery/jquery.js", :type => "text/javascript"}

解决方案

You can use the Ping class.

require 'resolv-replace'
require 'ping'

def internet_connection?
  Ping.pingecho "google.com", 1, 80
end

The method returns true or false and doesn't raise exceptions.

这篇关于检查Internet连接使用Ruby的存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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