All pastes #979630 Raw Edit

Anonymous

public ruby v1 · immutable
·published 2008-04-10 14:10 UTC
rendered paste body
class IpAddress < ActiveRecord::Base  belongs_to  :subnet  belongs_to  :interface  def address_string    IpAddress.long2ip(address)  end  def address_string= (ip)    print "received ip: " << (ip==nil ? "nil" : ip) << "\n"    address= IpAddress.ip2long(ip)    print "address now equals: " << address.to_s << "\n"  end  def IpAddress.ip2long (ip)    long = 0    ip.split(/\./).each_with_index do |b, i|      if(b.to_i>255)        #invalid octet.        return      end      if(b.to_i<0)        #invalid octet.        return      end      long += b.to_i << (8*i)    end    long  end  def IpAddress.long2ip (long)    if long == -1      return    end      ip = []      4.times do |i|        ip.push(long.to_i & 255)        long = long.to_i >> 8      end      ip.join(".")  endend--- script/console output:ksmith@ksmith-desktop:~/secnetdb_root$ script/consoleLoading development environment (Rails 2.0.2)>> x= IpAddress.new=> #<IpAddress id: nil, address: nil, interface_id: nil, subnet_id: nil, comment: "">>> x.address=> nil>> x.address = 1=> 1>> x.address=> 1>> x.address_string = "10.17.184.1"received ip: 10.17.184.1address now equals: 28840202=> "10.17.184.1">> x.address=> 1>> x.address_string=> "1.0.0.0">> x.address = 28840202=> 28840202>> x.address_string=> "10.17.184.1"