Class: Trema::IP
- Inherits:
-
Object
- Object
- Trema::IP
- Defined in:
- ruby/trema/ip.rb
Overview
A wrapper class to IPAddr
Instance Attribute Summary collapse
-
#value ⇒ IPAddr
readonly
Value object instance of proxied IPAddr.
Instance Method Summary collapse
-
#initialize(addr) ⇒ IP
constructor
Creates a IP instance object as a proxy to IPAddr class.
-
#prefixlen ⇒ Number
Prefixlen of IPv4 address.
-
#to_a ⇒ Array
(also: #to_array)
An array of decimal numbers converted from IP address.
-
#to_i ⇒ Number
The IPv4 address in its numeric representation.
-
#to_s ⇒ String
The IPv4 address in its text representation.
Constructor Details
#initialize(addr) ⇒ IP
Creates a Trema::IP instance object as a proxy to IPAddr class.
50 51 52 53 54 55 56 57 58 59 |
# File 'ruby/trema/ip.rb', line 50 def initialize addr, prefixlen = 32 if !addr.kind_of? String @value = IPAddr.new( addr, Socket::AF_INET ) else @value = IPAddr.new( addr ) end if prefixlen < 32 @value = @value.mask( prefixlen ) end end |
Instance Attribute Details
#value ⇒ IPAddr (readonly)
Returns value object instance of proxied IPAddr.
30 31 32 |
# File 'ruby/trema/ip.rb', line 30 def value @value end |
Instance Method Details
#prefixlen ⇒ Number
Returns prefixlen of IPv4 address.
65 66 67 68 69 70 71 72 73 74 |
# File 'ruby/trema/ip.rb', line 65 def prefixlen range = @value.to_range mask = range.first.to_i ^ range.last.to_i masklen = 0 while mask != 0 do mask = mask >> 1 masklen += 1 end return 32 - masklen end |
#to_a ⇒ Array Also known as: to_array
Returns an array of decimal numbers converted from IP address.
97 98 99 100 101 |
# File 'ruby/trema/ip.rb', line 97 def to_a to_s.split( "." ).collect do | each | each.to_i end end |
#to_i ⇒ Number
Returns the IPv4 address in its numeric representation.
88 89 90 |
# File 'ruby/trema/ip.rb', line 88 def to_i @value.to_i end |
#to_s ⇒ String
Returns the IPv4 address in its text representation.
80 81 82 |
# File 'ruby/trema/ip.rb', line 80 def to_s @value.to_s end |