-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a method to convert from IPAddr to IP #19
Comments
I wrote some methods making this possible. The pull is pending but you can give it a try here: https://github.com/martin-schmidt/ruby-ip/tree/feature-to_hton I need the code to store IPs in a database, but it can also be used to convert from # new IPAddr
ipaddr = IPAddr.new('192.168.2.1')
# => #<IPAddr: IPv4:192.168.2.1/255.255.255.255>
# convert it to IP
ip_from_ipaddr = IP.new_ntoh(ipaddr.hton)
# => <IP::V4 192.168.2.1>
# and back to IPAddr, because we can
ipaddr_from_ip = IPAddr.new_ntoh(ip_from_ipaddr.hton)
# => #<IPAddr: IPv4:192.168.2.1/255.255.255.255>
|
I don't like the names "ntoh" and "hton", as these have different and widely-known meanings in the socket world. ("ntoh" means convert from network byte order to host byte order, and "hton" is the opposite). However if the IPAddr library already You can already do the following:
But this requires you to know the address family. So it seems what you are really suggesting is two things:
Looking at source, For ruby-ip I'd want a new version of |
I find the ruby-ip functionality much better than the standard library's IPAddr but for example Rails converts ip adddresses automatically to IPAddr. It would be very useful if this gem had a way to convert from IPAddr to IP.
E.g. something like
The text was updated successfully, but these errors were encountered: