[#4766] Wiki — "Glen Stampoultzis" <trinexus@...>

21 messages 2000/09/04
[#4768] RE: Wiki — "NAKAMURA, Hiroshi" <nahi@...> 2000/09/04

Hi, Glen,

[#4783] Re: Wiki — Masatoshi SEKI <m_seki@...> 2000/09/04

[#4785] Re: Wiki — "NAKAMURA, Hiroshi" <nakahiro@...> 2000/09/05

Howdy,

[#4883] Re-binding a block — Dave Thomas <Dave@...>

16 messages 2000/09/12

[#4930] Perl 6 rumblings -- RFC 225 (v1) Data: Superpositions — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/09/15

[#4936] Ruby Book Eng. translation editor's questions — Jon Babcock <jon@...>

20 messages 2000/09/16

[#5045] Proposal: Add constants to Math — Robert Feldt <feldt@...>

15 messages 2000/09/21

[#5077] Crazy idea? infix method calls — hal9000@...

This is a generalization of the "in" operator idea which I

17 messages 2000/09/22

[#5157] Compile Problem with 1.6.1 — Scott Billings <aerogems@...>

When I try to compile Ruby 1.6.1, I get the following error:

15 messages 2000/09/27

[ruby-talk:5114] Types and ===

From: hal9000@...
Date: 2000-09-25 23:40:01 UTC
List: ruby-talk #5114
<sigh> I imagine Yoda behind me, shaking his little green head
and saying, "You will never be a Jedi..."

I know that the case statement uses the relationship operator ===;
and I know it's different for different objects; and I know it's not
commutative; and I know it's not ==.

But still, one thing is a little counter-intuitive to me.

Reason with me: Normally when x == y, x === y is also true (I'm
not saying the converse!!). But I have found a case where it isn't.
I can see there may be others.)

See the fragment below, and its output.

classify1 and classify2 don't do the same thing. The first fails;
the second works. classify3 also works.

Comments, anyone?

HF


def classify1(arg)
  case arg.type
    when String
      print "  arg is a string\n"
    when Array
      print "  arg is an array\n"
    when Hash
      print "  arg is a hash\n"
    else
      print "  arg is unknown\n"
  end
end

def classify2(arg)
  if arg.type == String
      print "  arg is a string\n"
  elsif arg.type == Array
      print "  arg is an array\n"
  elsif arg.type == Hash
      print "  arg is a hash\n"
  else
      print "  arg is unknown\n"
  end
end

def classify3(arg)
  case arg.type.to_s
    when String.to_s
      print "  arg is a string\n"
    when Array.to_s
      print "  arg is an array\n"
    when Hash.to_s
      print "  arg is a hash\n"
    else
      print "  arg is unknown\n"
  end
end


hash =  {"abc"=>"def", "ghi"=>"jkl", "mno"=>"pqr"}
str  =  "foo"
arr  =  [1,2,3]

print "\nclassify1:\n"
classify1(arr)
classify1(str)
classify1(hash)

print "\nclassify2:\n"
classify2(arr)
classify2(str)
classify2(hash)

print "\nclassify3:\n"
classify3(arr)
classify3(str)
classify3(hash)
# End of code

And the output:

classify1:
  arg is unknown
  arg is unknown
  arg is unknown

classify2:
  arg is an array
  arg is a string
  arg is a hash

classify3:
  arg is an array
  arg is a string
  arg is a hash


--
Hal Fulton


Sent via Deja.com http://www.deja.com/
Before you buy.

In This Thread

Prev Next