Type

From: Noixe [mailto:Noixe@email.it]
Subject: Type

Is there a method to know a type of object?

$ri Object#class

This is a test ‘ri’. Please report errors and omissions
on http://www.rubygarden.org/ruby?RIOnePointEight

----------------------------------------------------------- Object#class
obj.class → aClass

···
 Returns the class of obj, now preferred over Object#type, as an
 object's type in Ruby is only loosely tied to that object's class.
 This method must always be called with an explicit receiver, as
 class is also a reserved word in Ruby.
    1.class      #=> Fixnum
    self.class   #=> Object

For example:

x = 1
y = [1, 2, 3]
z = {1 => “one”, 2 => “two”}

puts x.type # Must print “int”
puts x.type # Must print “array”
puts x.type # Must print “hash”

puts ({1 => “2”}).class
Hash
=> nil
puts [1, 2, 3].class
Array
=> nil
puts 1.class
Fixnum
=> nil

Thanks

Noixe

gavri