mallowigi
7/3/2016 - 5:07 PM

Ipaddr.rb

Ipaddr.rb

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