Type

Hello,

I’m italian, then sorry in advance for my bad english:

Is there a method to know a type of 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”

Thanks

···


Noixe

Hello,

···

On Thu, 29 Jan 2004, Noixe wrote:

I’m italian, then sorry in advance for my bad english:

Is there a method to know a type of object?

For example:

Did you try actually running this code? If so, you wouldn’t have had to
post the question:

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

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

NOTE: HAD TO FIX THE ABOVE LINES DUE TO TYPO

</heh.rb>

ruby heh.rb

heh.rb:6: warning: Object#type is deprecated; use Object#class
Fixnum
heh.rb:7: warning: Object#type is deprecated; use Object#class
Array
heh.rb:8: warning: Object#type is deprecated; use Object#class
Hash

…so this is telling you that 1) your code worked, but 2) the #type
method has been deprecated and you should use #class instead.

Chad

In article cee6b908.0401281235.5d7f1ea8@posting.google.com,

Hello,

I’m italian, then sorry in advance for my bad english:

Is there a method to know a type of 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”

you can call class e.g.

[mike@ratdog mike]$ irb

x = 1
=> 1
y = [1, 2, 3]
=> [1, 2, 3]
z = {1 => “one”, 2 => “two”}
=> {1=>“one”, 2=>“two”}
x.class
=> Fixnum
y.class
=> Array
z.class
=> Hash

Hope this helps,

Mike

···

Noixe Noixe@email.it wrote:


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

I’m italian, then sorry in advance for my bad english:

Welcome to Ruby. You don’t have to excuse… your English is ok.

Is there a method to know a type of object?
[snip]

IIRC (If I Remember Correct) Object#type is deprecated…
Instead you may use #class. Like this:

irb
irb(main):001:0> x = 1
=> 1
irb(main):002:0> x.class
=> Fixnum
irb(main):003:0> y = [1, 2, 3]
=> [1, 2, 3]
irb(main):004:0> y.class
=> Array
irb(main):005:0> z = {1 => “one”, 2 => “two”}
=> {1=>“one”, 2=>“two”}
irb(main):006:0> z.class
=> Hash
irb(main):007:0>

···

On Wed, 28 Jan 2004 12:35:45 -0800, Noixe wrote:


Simon Strandgaard

Noixe wrote:

Is there a method to know a type of 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”

You made a very good guess :wink:

puts x.type # prints "Fixnum"
puts y.type # prints "Array"
puts z.type # prints "Hash"

Note that for Ruby 1.8, the type() method is deprecated and you should
use class() instead, i.e.

puts x.class # prints "Fixnum"
puts y.class # prints "Array"
puts z.class # prints "Hash"

Hope this helps,

Lyle