views:

347

answers:

3

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

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

+1  A: 

There's always:

require 'socket'
...
Socket.gethostname

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

Wayne Conrad
A: 

Use backticks and the command hostname

current_host = `hostname`

This sends the command to the shell, and returns the hostname. Works on at least: Debian Linux, Windows, Solaris.

btelles
+1  A: 

All you have to do is look at the request object in your controller:

request.host_with_port

or if you don't want the port, just

request.host
Wilhelm