# Ipaddr

**URL:** <https://www.ruby-forum.com/t/ipaddr/115874>\
**Category:** Development\
**Created:** [September 29, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/ipaddr/115874 "2007-09-29T17:29:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kazuhiro\_NISHIYAMA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/77aa72/32.png) [@Kazuhiro\_NISHIYAMA](https://www.ruby-forum.com/u/Kazuhiro_NISHIYAMA)\
**Post date:** [September 29, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/ipaddr/115874/1 "2007-09-29T17:29:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kazuhiro\_NISHIYAMA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/77aa72/32.png) [@Kazuhiro\_NISHIYAMA](https://www.ruby-forum.com/u/Kazuhiro_NISHIYAMA)\
**Post date:** [October 2, 2007, 1:17pm UTC](https://www.ruby-forum.com/t/ipaddr/115874/2 "2007-10-02T13:17:55Z")

</div>

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](http://Akinori.org) / [MUSHA.org](http://MUSHA.org)  
/ ) ) ) ) / [FreeBSD.org](http://FreeBSD.org) / [Ruby-lang.org](http://Ruby-lang.org)  
Akinori MUSHA aka / (\_ / ( (\_\_( @ [iDaemons.org](http://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”
