earlyadopter
earlyadopter

Reputation: 1547

how do I convert int32 IP address into octets in ruby?

I'm looking for the opposite to this:

require 'ipaddr'
ip = IPAddr.new "10.0.2.15"
ip.to_i                      # 167772687

I need to pass something like 167772687, and want to get back "10.0.2.15".

This:

ip = IPAddr.new 167772687

returns the error:

IPAddr::AddressFamilyError: address family must be specified

How to I specify that I'm passing int32?

Upvotes: 1

Views: 292

Answers (1)

earlyadopter
earlyadopter

Reputation: 1547

OK, found the answer:

require 'ipaddr'

ip = IPAddr.new(167772687, family = Socket::AF_INET)
ip.to_s   # "10.0.2.15"

Upvotes: 1

Related Questions