How do I get the server name?

Hi,

I’m using Ruby 1.8. How do I get my server name? I’m running Apache
2 on Fedora Core 6 Linux. I tried “ENV[“server_name”]”, but got an
error.

  • Dave

Since ENV is a hash, why don’t you open up irb and type

ENV.each { |key, value| puts “#{lkey} - #{value}” }

It’s a bit quicker than expecting a reply on a mailing list.

On Sun, Mar 2, 2008 at 11:49 PM, [email protected]

Andrei M. wrote:

Since ENV is a hash

No, it’s not.

ENV.is_a? Hash
=> false

On Mon, Mar 3, 2008 at 12:36 AM, Jano S. [email protected]
wrote:

Of course, it’s not a real Hash instance, but you can work with it as
though it was a Hash.

I was actually surprised to see that ENV.class returns Object. It
certainly acts like a Hash so…

ENV.each { |key, value| puts “#{lkey} - #{value}” }

There’s a typo: not #{lkey}, byt #{key}

Right. Sorry about that :slight_smile:

On Sun, Mar 2, 2008 at 11:00 PM, Sebastian H.
[email protected] wrote:

Andrei M. wrote:

Since ENV is a hash

No, it’s not.

ENV.is_a? Hash
=> false

It quacks like a hash, so it must be a hash :wink:
Of course, it’s not a real Hash instance, but you can work with it as
though it was a Hash.

ENV.each { |key, value| puts “#{lkey} - #{value}” }

There’s a typo: not #{lkey}, byt #{key}

Jano

On Sun, Mar 2, 2008 at 11:42 PM, Andrei M. [email protected]
wrote:

I was actually surprised to see that ENV.class returns Object. It
certainly acts like a Hash so…

http://ruby-doc.org/docs/ProgrammingRuby/html/rubyworld.html#S3

On Mar 2, 2008, at 1:49 PM, [email protected] wrote:

Hi,

I’m using Ruby 1.8. How do I get my server name? I’m running Apache
2 on Fedora Core 6 Linux. I tried “ENV[“server_name”]”, but got an
error.

  • Dave

hostname.chomp

How about

require “socket”

host_name = Socket::gethostname

RF

On Mar 2, 4:48 pm, Jano S. [email protected] wrote:

On Sun, Mar 2, 2008 at 11:42 PM, Andrei M. [email protected] wrote:

I was actually surprised to see that ENV.class returns Object. It
certainly acts like a Hash so…

http://ruby-doc.org/docs/ProgrammingRuby/html/rubyworld.html#S3

Thanks all for your replies but there’s something odd. I guess I’m
still confused about how to access values from this thing that is not
quite a hash. In irb, I did as Andrei suggested and thought I saw a
HOSTNAME key, but when I try and print out the value (“puts
ENV[‘HOSTNAME’]”), I get nil. Why does it appear when I print out the
hash but not when I try and access the value individually?

Here’s the irb action:

irb(main):068:0* ENV.each { |key, value| puts “#{key} - #{value}” }
USER - myusername
LOGNAME - myusername
HOME - /home/myusername
PATH - /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/myusername/
bin
MAIL - /var/spool/mail/myusername
SHELL - /bin/zsh
SSH_CLIENT - 67.190.94.163 61497 22
SSH_CONNECTION - 67.190.94.163 61497 192.168.1.99 22
SSH_TTY - /dev/pts/1
TERM - xterm
SHLVL - 1
PWD - /usr/local/apache2/htdocs/easyrx/app/controllers
OLDPWD - /usr/local/apache2/htdocs/easyrx/app
HOSTNAME - mydomain.com
HISTSIZE - 1000
INPUTRC - /etc/inputrc
LS_COLORS -
no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:.cmd=00;32:.exe=00;32:.com=00;32:.btm=00;32:.bat=00;32:.sh=00;32:.csh=00;32:.tar=00;31:.tgz=00;31:.arj=00;31:.taz=00;31:.lzh=00;31:.zip=00;31:.z=00;31:.Z=00;31:.gz=00;31:.bz2=00;31:.bz=00;31:.tz=00;31:.rpm=00;31:.cpio=00;31:.jpg=00;35:.gif=00;35:.bmp=00;35:.xbm=00;35:.xpm=00;35:.png=00;35:.tif=00;35:
CVS_RSH - ssh
CVSROOT - /usr/local/cvs-repository
G_BROKEN_FILENAMES - 1
SSH_ASKPASS - /usr/libexec/openssh/gnome-ssh-askpass
KDEDIR - /usr
LANG - en_US.UTF-8
LESSOPEN - |/usr/bin/lesspipe.sh %s
QTDIR - /usr/lib/qt-3.3
QTINC - /usr/lib/qt-3.3/include
QTLIB - /usr/lib/qt-3.3/lib
JAVA_HOME - /usr/lib/jdk
CATALINA_HOME - /usr/local/tomcat
_ - /usr/local/bin/irb
LINES - 46
COLUMNS - 157
=> {“USER”=>“myusername”, “LOGNAME”=>“myusername”, “HOME”=>"/home/
myusername", “PATH”=>"/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/
home/myusername/bin", “MAIL”=>"/var/spool/mail/myusername", “SHELL”=>"/
bin/zsh", “SSH_CLIENT”=>“67.190.94.163 61497 22”,
“SSH_CONNECTION”=>“67.190.94.163 61497 192.168.1.99 22”, “SSH_TTY”=>"/
dev/pts/1", “TERM”=>“xterm”, “SHLVL”=>“1”, “PWD”=>"/usr/local/apache2/
htdocs/easyrx/app/controllers", “OLDPWD”=>"/usr/local/apache2/htdocs/
easyrx/app", “HOSTNAME”=>“mydomain.com”, “HISTSIZE”=>“1000”,
“INPUTRC”=>"/etc/inputrc",
“LS_COLORS”=>“no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:.cmd=00;32:.exe=00;32:.com=00;32:.btm=00;32:.bat=00;32:.sh=00;32:.csh=00;32:.tar=00;31:.tgz=00;31:.arj=00;31:.taz=00;31:.lzh=00;31:.zip=00;31:.z=00;31:.Z=00;31:.gz=00;31:.bz2=00;31:.bz=00;31:.tz=00;31:.rpm=00;31:.cpio=00;31:.jpg=00;35:.gif=00;35:.bmp=00;35:.xbm=00;35:.xpm=00;35:.png=00;35:.tif=00;35:”,
“CVS_RSH”=>“ssh”, “CVSROOT”=>"/usr/local/cvs-repository",
“G_BROKEN_FILENAMES”=>“1”, “SSH_ASKPASS”=>"/usr/libexec/openssh/gnome-
ssh-askpass", “KDEDIR”=>"/usr", “LANG”=>“en_US.UTF-8”, “LESSOPEN”=>"|/
usr/bin/lesspipe.sh %s", “QTDIR”=>"/usr/lib/qt-3.3", “QTINC”=>"/usr/
lib/qt-3.3/include", “QTLIB”=>"/usr/lib/qt-3.3/lib", “JAVA_HOME”=>"/
usr/lib/jdk", “CATALINA_HOME”=>"/usr/local/tomcat", “_”=>"/usr/local/
bin/irb", “LINES”=>“46”, “COLUMNS”=>“157”}

laredotornado wrote:

Thanks all for your replies but there’s something odd. I guess I’m
still confused about how to access values from this thing that is not
quite a hash. In irb, I did as Andrei suggested and thought I saw a
HOSTNAME key, but when I try and print out the value (“puts
ENV[‘HOSTNAME’]”), I get nil. Why does it appear when I print out the
hash but not when I try and access the value individually?

Here’s the irb action:

irb(main):068:0* ENV.each { |key, value| puts “#{key} - #{value}” }
USER - myusername
LOGNAME - myusername
HOME - /home/myusername
PATH - /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/myusername/
bin
MAIL - /var/spool/mail/myusername
SHELL - /bin/zsh
SSH_CLIENT - 67.190.94.163 61497 22
SSH_CONNECTION - 67.190.94.163 61497 192.168.1.99 22
SSH_TTY - /dev/pts/1
TERM - xterm
SHLVL - 1
PWD - /usr/local/apache2/htdocs/easyrx/app/controllers
OLDPWD - /usr/local/apache2/htdocs/easyrx/app
HOSTNAME - mydomain.com
HISTSIZE - 1000
INPUTRC - /etc/inputrc
LS_COLORS -

Why did you think leaving out the:

puts ENV[‘HOSTNAME’]

as the next statement would help prove your point?

i use request.env[‘SERVER_NAME’]

This seems to work also:

uname -n.chop

W

[email protected] wrote in post #640566:

I’m using Ruby 1.8. How do I get my server name? I’m running Apache
2 on Fedora Core 6 Linux. I tried “ENV[“server_name”]”, but got an
error.

You shouldn’t get an error - it should give nil for unknown key.

If you’re using erb templates, try something like

<%= ENV.inspect %>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs