lib/ipaddress/prefix.rb in ipaddress-0.5.0 vs lib/ipaddress/prefix.rb in ipaddress-0.6.0
- old
+ new
@@ -9,11 +9,11 @@
#
# Parent class for Prefix32 and Prefix128
#
# =DESCRIPTION
#
- # IPAddresS::Prefix is the parent class for IPAddress::Prefix32
+ # IPAddress::Prefix is the parent class for IPAddress::Prefix32
# and IPAddress::Prefix128, defining some modules in common for
# both the subclasses.
#
# IPAddress::Prefix shouldn't be accesses directly, unless
# for particular needs.
@@ -22,26 +22,63 @@
include Comparable
attr_reader :prefix
+ #
+ # Creates a new general prefix
+ #
def initialize(num)
@prefix = num.to_i
end
+ #
+ # Returns a string with the prefix
+ #
def to_s
"#@prefix"
end
alias_method :inspect, :to_s
+ #
+ # Returns the prefix
+ #
def to_i
@prefix
end
+ #
+ # Compare the prefix
+ #
def <=>(oth)
@prefix <=> oth.to_i
end
+ #
+ # Sums two prefixes or a prefix to a
+ # number, returns a Fixnum
+ #
+ def +(oth)
+ if oth.is_a? Fixnum
+ self.prefix + oth
+ else
+ self.prefix + oth.prefix
+ end
+ end
+
+ #
+ # Returns the difference between two
+ # prefixes, or a prefix and a number,
+ # as a Fixnum
+ #
+ def -(oth)
+ if oth.is_a? Fixnum
+ self.prefix - oth
+ else
+ (self.prefix - oth.prefix).abs
+ end
+ end
+
end # class Prefix
class Prefix32 < Prefix