开发者

Translate one line please from python to ruby

Need to calculate IP => int just like in here:

https://bitbucket.org/lorien/django-ipgeo/src/d19648c9b14f/ipgeo/models.py but in ruby.

I'm pretty new in ruby, so cannot translate this into ruby:

number = struct.unpack('!L', socket.inet_aton(ip))[0]

struct, socket are python std, ip is a string li开发者_运维百科ke '127.0.0.1'


Ruby comes with an ipaddr module that specifies an IPAddr class that can return a representation as an integer.

require 'ipaddr'

addr = IPAddr.new '127.0.0.1'
print addr.to_i

Here it is in irb:

ruby-1.9.2-p290 :002 > require 'ipaddr'
 => true 
ruby-1.9.2-p290 :003 > addr = IPAddr.new '127.0.0.1'
 => #<IPAddr: IPv4:127.0.0.1/255.255.255.255> 
ruby-1.9.2-p290 :004 > addr.to_i
 => 2130706433 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜