Peter Fitzgibbons wrote:
> HI All,
> 
> Could somone explain the subtle difference between Module#=== and
> Object#is_a?
> 
> I found a usage of === to test if an argument is Regexp or not... just 
> want
> to know the restrictions.
> The code is beautiful...
> 
> if Regexp === regexp_or_string

The Class class defines === operator in such a way that it can be used 
in place of is_a.
 +---------------------------------------------+
 |  x = 1                                      |
 |  Numeric === x   # => true: x is_a Numeric  |
 +---------------------------------------------+
This is exactly the same use as your:
 +---------------------------------------------+
 |  if Regexp === regexp_or_string             |
 +---------------------------------------------+

All the technical details about Ruby implementation of === and is_a are 
pretty much irrelevant. What is important is the meaning of the test we 
perform with these and similar operators. As others already pointed out 
"===" and "is_a" are used to obtain exactly the same info about 
instances or classes. You can find the best explanation about this in 
the  defacto Ruby bible called "The Ruby Programming Language" by D. 
Flanagan and Y. Matsumoto, in chapter "8.1 Types, Clases and Modules". 
Let me copy it out here:

The most commonly used reflective methods are those for determining the 
type of an object - what class it is an instance of, and what methods it 
responds to. ... To review:

o.class
    Returns the class of an object o.
c.superclass
    Returns the superclass of a class c.
o.instance_of? c
    Determines whether the object o.class == c.
o.is_a? c
    Determines whether o is an instance of c, or of any of its 
subclasses. If c is a module, this method tests wether o.class (or any 
of its ancestors) includes the module.
o.kind_of? c
    kind_of? is a synonym for is_a?
c === o
    For any class or module c, determines if o.is_a?(c)
o.responds_to? name
    Determines whether the object o has a public or protected method 
with the specified name. Pass true as the second argument to check 
private methods as well.  i.e: o.responds_to?("name", true)
-- 
Posted via http://www.ruby-forum.com/.