Method: NetAddr::CIDR#cmp

Defined in:
lib/cidr.rb

#cmp(cidr) ⇒ Object

Synopsis

Compare the current CIDR with a provided CIDR and return:

  • 1 if the current CIDR contains (is supernet of) the provided CIDR

  • 0 if the current CIDR and the provided CIDR are equal (base address and netmask are equal)

  • -1 if the current CIDR is contained by (is subnet of) the provided CIDR

  • nil if the two CIDR addresses are unrelated

Example: cidr = NetAddr::CIDR.create(‘192.168.1.0/24’) cidr.cmp(‘192.168.1.0/25’) => 1 cidr.cmp(‘192.168.1.0/24’) => 0 cidr.cmp(‘192.168.0.0/23’) => -1 cidr.cmp(‘10.0.0.0/24’) => nil

Arguments:

  • CIDR address or NetAddr::CIDR object

Returns:

  • Integer or nil



671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# File 'lib/cidr.rb', line 671

def cmp(cidr)
        if (!cidr.kind_of?(NetAddr::CIDR))
            begin
                cidr = NetAddr::CIDR.create(cidr)
            rescue Exception => error
                raise ArgumentError, "Provided argument raised the following " +
                                     "errors: #{error}"
            end
        end

        if (cidr.version != @version)
            raise VersionError, "Attempted to compare a version #{cidr.version} CIDR " +
                                 "with a version #{@version} CIDR."
        end

        # compare
        comparasin = NetAddr.cidr_compare(self,cidr)

    return(comparasin)
end