从Rails控制器获取主机名 [英] Get hostname from Rails controller

查看:65
本文介绍了从Rails控制器获取主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从控制器获取运行Rails应用程序的机器的主机名.

I am trying to get the hostname of the machine which a rails application is running on from the controller.

考虑到它应该在Windows和Linux上都可以工作的情况,最好的方法是什么?

What would be the best way to do this taking into account it should work on both windows and linux?

推荐答案

总有:

require 'socket'
...
Socket.gethostname

我没有Windows盒子可以用来测试,但是文档没有提到它是* nix专用的.

I've got no Windows box handy with which to test this, but the docs make no mention of it being *nix specific.

注意:在Rails 4以及其他版本的Rails中,require语句不是必需的.如果您要编写没有Rails的纯Ruby,则必须使用它.

Note: The require statement is not necessary for Rails 4, and probably other Rails versions as well. It is required if you are doing plain Ruby without Rails.

这篇关于从Rails控制器获取主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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