Class: IpRanges::Ip

Inherits:
Object
  • Object
show all
Defined in:
lib/ip-ranges/ip.rb

Overview

Class to represent a single IP number. Exposes methods for comparisons with other IP numbers, as well as a method to increment the IP number.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Ip

Arguments:

  • A hash containing a single key whose value is the IP number as a string

Example:

Ip.new(:number => '1.2.3.4')


23
24
25
# File 'lib/ip-ranges/ip.rb', line 23

def initialize(params = {})
  @number = params[:number]
end

Instance Attribute Details

#numberObject

String representation of this Ip number

Example:

ip = Ip.new :number => '1.2.3.4'
ip.number => '1.2.3.4'


13
14
15
# File 'lib/ip-ranges/ip.rb', line 13

def number
  @number
end

Instance Method Details

#==(ip) ⇒ Object

Arguments:

  • another Ip object

Example:

ip1 = Ip.new(:number => '1.2.3.4')
ip2 = Ip.new(:number => '1.2.3.5')
ip3 = Ip.new(:number => '1.2.3.4')

ip1 == ip2 => false
ip1 == ip3 => true


45
46
47
# File 'lib/ip-ranges/ip.rb', line 45

def ==(ip)
  number == ip.number
end

#>(ip) ⇒ Object

Arguments:

  • another Ip object

Example:

ip1 = Ip.new(:number => '1.2.3.4')
ip2 = Ip.new(:number => '1.2.3.5')

ip1 > ip2 => false
ip2 > ip1 => true
ip2 > ip2 => false


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ip-ranges/ip.rb', line 78

def >(ip)
  a1, b1, c1, d1 = tuples number
  a2, b2, c2, d2 = tuples ip.number
  if a1 > a2
    true
  elsif a1 < a2
    false
  elsif b1 > b2
    true
  elsif b1 < b2
    false
  elsif c1 > c2
    true
  elsif c1 < c2
    false
  else
    d1 > d2
  end
end

#>=(ip) ⇒ Object

Arguments:

  • another Ip object

Example:

ip1 = Ip.new(:number => '1.2.3.4')
ip2 = Ip.new(:number => '1.2.3.5')
ip3 = Ip.new(:number => '1.2.3.4')

ip1 >= ip2 => false
ip2 >= ip3 => true
ip1 >= ip3 => true


62
63
64
# File 'lib/ip-ranges/ip.rb', line 62

def >=(ip)
  self == ip || self > ip
end

#decrementObject

Changes the ‘number’ property of this Ip to the number before this one.

example:

  ip = Ip.new(:number => '1.2.3.4')
  ip.number => '1.2.3.4'
  ip.decrement
  ip.number => '1.2.3.3'


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ip-ranges/ip.rb', line 108

def decrement
  a, b, c, d = tuples number
  if d > 0
    d -= 1
  else
    d = 255
    if c > 0
      c -= 1
    else
      c = 255
      if b > 0
        b -= 1
      else
        b = 255
        if a > 0
          a -= 1
        else
          raise "No more IPs"
        end
      end
    end
  end
  @number = [a, b, c, d].join('.')
end

#incrementObject

Changes the ‘number’ property of this Ip to the next number after this one.

example:

  ip = Ip.new(:number => '1.2.3.4')
  ip.number => '1.2.3.4'
  ip.increment
  ip.number => '1.2.3.5'


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ip-ranges/ip.rb', line 143

def increment
  a, b, c, d = tuples number
  if d < 255
    d += 1
  else
    d = 0
    if c < 255
      c += 1
    else
      c = 0
      if b < 255
        b += 1
      else
        b = 0
        if a < 255
          a += 1
        else
          raise "No more IPs"
        end
      end
    end
  end
  @number = [a, b, c, d].join('.')
end

#to_sObject

Example: Ip.new(:number => ‘1.2.3.4’).to_s => ‘1.2.3.4’



29
30
31
# File 'lib/ip-ranges/ip.rb', line 29

def to_s
  number
end