2

For some time I was using code:

hostname = Socket.gethostbyname(Socket.gethostname).first 

taken from here.

But, as the comment said - it's deprecated, and now both rubocop and ruby itself are complaining:

=$ ./z.rb 
./z.rb:5: warning: Socket.gethostbyname is deprecated; use Addrinfo.getaddrinfo instead.

=$ rubocop z.rb
...
z.rb:5:13: W: Lint/DeprecatedClassMethods: Socket.gethostbyname is deprecated in favor of Addrinfo#getaddrinfo.
puts Socket.gethostbyname(Socket.gethostname).first
            ^^^^^^^^^^^^^

The problem is that I don't know how to use getaddrinfo to get the same information. Anyone could show an example?

For now, I figured I'll just use:

`hostname --fqdn`

which works, as I only run it on Linux, but it's not really nice approach.

chooboo
  • 51
  • 5

1 Answers1

3

Got help in some other way, but figured others might benefit too.

Current way to do it is:

require 'socket'
Addrinfo.getaddrinfo(Socket.gethostname, nil).first.getnameinfo.first
Ludovic Kuty
  • 4,868
  • 3
  • 28
  • 42
chooboo
  • 51
  • 5