Last active
July 3, 2016 17:11
-
-
Save mallowigi/813e04080d2ced526f0fe1dd6872a0c1 to your computer and use it in GitHub Desktop.
Ipaddr.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ipaddr' | |
# Builtin | |
def ip_to_int32(ip) | |
x = IPAddr.new(ip).to_i | |
end | |
def ip_to_int32(ip) | |
ip.split( '.' ).reduce( 0 ) { |total, p| total * 256 + p.to_i } | |
end | |
# Using format | |
def ip_to_int32(ip) | |
("%02x%02x%02x%02x" % ip.split('.')).to_i(16) | |
end | |
# Using binary | |
def ip_to_int32(ip) | |
ip.split(".").inject(0) { |memo, val| (memo << 8) + val.to_i } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment