[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How do I get the server name?

laredotornado@zipmail.com

3/2/2008 9:46:00 PM

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
11 Answers

Andrei Maxim

3/2/2008 9:57:00 PM

0

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, laredotornado@gmail.com
<laredotornado@gmail.com> 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
>
>



--
Andrei Maxim
http://andr...

Sebastian Hungerecker

3/2/2008 10:01:00 PM

0

Andrei Maxim wrote:
> Since ENV is a hash

No, it's not.
>> ENV.is_a? Hash
=> false

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Jano Svitok

3/2/2008 10:37:00 PM

0

On Sun, Mar 2, 2008 at 11:00 PM, Sebastian Hungerecker
<sepp2k@googlemail.com> wrote:
> Andrei Maxim 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 ;-)
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

Andrei Maxim

3/2/2008 10:42:00 PM

0

On Mon, Mar 3, 2008 at 12:36 AM, Jano Svitok <jan.svitok@gmail.com> wrote:
> On Sun, Mar 2, 2008 at 11:00 PM, Sebastian Hungerecker
> <sepp2k@googlemail.com> wrote:
> > Andrei Maxim 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 ;-)
> 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 :)

--
Andrei Maxim
http://andr...

Jano Svitok

3/2/2008 10:49:00 PM

0

On Sun, Mar 2, 2008 at 11:42 PM, Andrei Maxim <andrei@andreimaxim.ro> 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/rubywor...

Steve Ross

3/2/2008 10:58:00 PM

0

On Mar 2, 2008, at 1:49 PM, laredotornado@gmail.com 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


(D. Alvarado)

3/2/2008 11:03:00 PM

0

On Mar 2, 4:48 pm, Jano Svitok <jan.svi...@gmail.com> wrote:
> On Sun, Mar 2, 2008 at 11:42 PM, Andrei Maxim <and...@andreimaxim.ro> 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/rubywor...

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"}

Ron Fox

3/3/2008 1:40:00 PM

0

How about

require "socket"
....

host_name = Socket::gethostname


RF
laredotornado wrote:
> On Mar 2, 4:48 pm, Jano Svitok <jan.svi...@gmail.com> wrote:
>> On Sun, Mar 2, 2008 at 11:42 PM, Andrei Maxim <and...@andreimaxim.ro> 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/rubywor...
>
> 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?
>

7stud --

3/4/2008 9:35:00 AM

0

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?
--
Posted via http://www.ruby-....

Phlip

3/4/2008 11:26:00 AM

0

> How about
> host_name = Socket::gethostname

or host_name = `hostname`.strip ?