Ipaddr

e$B@>;3OB9-$G$9!#e(B

ipaddre$B$Ge(B
IPAddr.new(“127.0.0.0/8”).include?(“127.0.0.1”)
e$B$,e(Bfalsee$B$K$J$C$FG:$s$@$3$H$,$"$k$N$G!"J8;zNs$b<+F0$GJQ49$7$Fe(B
e$B$&$1$D$1$F$/$l$k$H4r$7$$$N$G$O$J$$$+$H;W$$$^$9!#e(B

e$B:#$Oe(B

IPAddr.new(“127.0.0.0/8”).include?(IPAddr.new(“127.0.0.1”))

e$B$8$c$J$$$H$@$a!#e(B

e$B%i%$%V%i%j$N%a%s%F%J$NOC$G;W$$$@$7$?!#e(B

At Sun, 30 Sep 2007 00:27:59 +0900,
Kazuhiro NISHIYAMA wrote:

西山和広です。

ipaddrで
IPAddr.new(“127.0.0.0/8”).include?(“127.0.0.1”)
がfalseになって悩んだことがあるので、文字列も自動で変換して
うけつけてくれると嬉しいのではないかと思います。

今は

IPAddr.new(“127.0.0.0/8”).include?(IPAddr.new(“127.0.0.1”))

じゃないとだめ。

 こうするといいかな。

Index: ipaddr.rb

— ipaddr.rb (revision 13588)
+++ ipaddr.rb (working copy)
@@ -105,14 +105,26 @@
return s
end

  • def coerce(other)
  • case other
  • when IPAddr
  •  other
    
  • when String
  •  self.class.new(other)
    
  • else
  •  self.class.new(other, @family)
    
  • end
  • end
  • private coerce
  • Returns a new ipaddr built by bitwise AND.

    def &(other)
  • return self.clone.set(@addr & other.to_i)
  • return self.clone.set(@addr & coerce(other).to_i)
    end

Returns a new ipaddr built by bitwise OR.

def |(other)

  • return self.clone.set(@addr | other.to_i)
  • return self.clone.set(@addr | coerce(other).to_i)
    end

Returns a new ipaddr built by bitwise right-shift.

@@ -132,10 +144,7 @@

Returns true if two ipaddr are equal.

def ==(other)

  • if other.kind_of?(IPAddr) && @family != other.family
  •  return false
    
  • end
  • return (@addr == other.to_i)
  • return @family == other.family && @addr == coerce(other).to_i
    end

Returns a new ipaddr built by masking IP address with the given

@@ -153,6 +162,7 @@

p net1.include?(IPAddr.new(“192.168.2.255”)) #=> true

p net1.include?(IPAddr.new(“192.168.3.0”)) #=> false

def include?(other)

  • other = coerce(other)
    if ipv4_mapped?
    if (@mask_addr >> 32) != 0xffffffffffffffffffffffff
    return false
    @@ -165,17 +175,12 @@
    addr = @addr
    family = @family
    end
  • if other.kind_of?(IPAddr)
  •  if other.ipv4_mapped?
    
  • other_addr = (other.to_i & IN4MASK)
  • other_family = Socket::AF_INET
  •  else
    
  • other_addr = other.to_i
  • other_family = other.family
  •  end
    
  • else # Not IPAddr - assume integer in same family as us
  •  other_addr   = other.to_i
    
  •  other_family = family
    
  • if other.ipv4_mapped?

  •  other_addr = (other.to_i & IN4MASK)
    
  •  other_family = Socket::AF_INET
    
  • else

  •  other_addr = other.to_i
    
  •  other_family = other.family
    

    end

    if family != other_family

 ついでに <=> と succ を定義して Comparable にすると、 Range を
作ったりできますね。

ライブラリのメンテナの話で思いだした。

 梅本さんにCCしておきます。


/
/__ __ Akinori.org / MUSHA.org
/ ) ) ) ) / FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ / ( (__( @ iDaemons.org / and.or.jp

“Different eyes see different things,
Different hearts beat on different strings –
But there are times for you and me when all such things agree”