Sha256: 451bddabf2a1685959d457f4eba8a434b715284ed150bd0d5fc60dfac0d0c3bb

Contents?: true

Size: 636 Bytes

Versions: 22

Compression:

Stored size: 636 Bytes

Contents

require 'bindata'

# A custom type representing an IP address.
# The underlying binary representation is a sequence of four octets.
# The human accessible representation is a dotted quad.
class IPAddr < BinData::Primitive
  array :octets, :type => :uint8, :initial_length => 4

  def set(val)
    ints = val.split(/\./).collect { |int| int.to_i }
    self.octets = ints
  end

  def get
    self.octets.collect { |octet| "%d" % octet }.join(".")
  end
end

ip = IPAddr.new("127.0.0.1")

puts "human readable value:  #{ip}"                     #=> 127.0.0.1
puts "binary representation: #{ip.to_binary_s.inspect}" #=> "\177\000\000\001"

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
bindata-2.3.3 examples/ip_address.rb
bindata-2.3.2 examples/ip_address.rb
bindata-2.3.1 examples/ip_address.rb
bindata-2.3.0 examples/ip_address.rb
bindata-2.2.0 examples/ip_address.rb
bindata-2.1.0 examples/ip_address.rb
bindata-1.8.3 examples/ip_address.rb
jbangert-bindata-1.5.0 examples/ip_address.rb
bindata-2.0.0 examples/ip_address.rb
bindata-1.8.2 examples/ip_address.rb
bindata-1.8.1 examples/ip_address.rb
bindata-1.8.0 examples/ip_address.rb
bindata-1.6.0 examples/ip_address.rb
bindata-1.5.1 examples/ip_address.rb
bindata-1.5.0 examples/ip_address.rb
bindata-1.4.5 examples/ip_address.rb
bindata-1.4.4 examples/ip_address.rb
bindata-1.4.3 examples/ip_address.rb
bindata-1.4.2 examples/ip_address.rb
bindata-1.4.1 examples/ip_address.rb