I've found one question like this on stackoverflow, but it doesn't answer my question.
I'm following along with http://www.ruby-doc.org/docs/ProgrammingRuby/ to learn Ruby, but I'm running into problems with the example code and it's frustrating.
person = "Tim"
puts person.id
puts person.ty开发者_JS百科pe
puts person
The error message I'm getting is:
C:/Users/g3k/Desktop/Ruby/person.rb:2:in `<main>': undefined method `id' for "Tim":String (NoMethodError)
Obviously I'm running Windows (Vista) and I'm running ruby 1.9.2p0 (2010-08-18) [i386-mingw32]. I'm wondering my problem is because the book is outdated and Ruby has had some time to mature since this book came out (the second edition is available for purchase at this point)
I had the same problem with .id in a Jukebox example code, but I figured it was a fluke and continued. I understand what the error is, but I don't understand why.
id
is deprecated and replaced with object_id
.
type
is also deprecated. Use class
instead.
person = "Tim"
puts person.object_id
puts person.class
puts person
Output:
69284020
String
Tim
Refer to this for Object's methods.
Figured it out, I was right, .id is depreciated, .object_id is what should be used.
精彩评论