Reputation: 273
Working through an intro Ruby exercise with this code:
num = 8
7.times do
print num.type, " ", num, "\n"
num *= num
end
I keep getting:
NoMethodError: undefined method `type' for 8:Fixnum
Do i have to define type? I thought this was a method that ruby recognized
Upvotes: 2
Views: 2224
Reputation: 3
I had the same problem with method 'type' while working through Programming Ruby: The Pragmatic Programmer's Guide. The purpose of this exercise was to show that integers are stored in objects of classes Fixnum and Bignum, both subclasses of class Integer(Bignum for bigger numbers). Also, to show that Ruby automatically manages the conversion back and forth.
But since Feature #12005 in Ruby 2.4, Fixnum and Bignum have been unified into Integer. With them gone, the Object#type method, too, was gone. Note that Object#class method, will not show the distinction between Fixnum and Bignum in this exercise(recognizes both as Integer). So, yeah, the only thing this exercise will teach us now is a bit of history about Ruby.
If you want to know more about these two classes, check the first exercise on 'Standard Types' from the book.
Upvotes: 0
Reputation: 84114
The type
method used to return an object's class but was deprecated a long time ago (back in the 1.8 days) and subsequently removed.
You can use the class
method instead, however if you are following a tutorial or something similar this is a sign that it is very old - possibly 10 years old!
Upvotes: 5
Reputation: 10825
By the type
you probably meant class
, so change type
to class
:
num = 8
7.times do
print num.class, " ", num, "\n"
num *= num
end
Upvotes: 2