开发者

undefined method `id' in simple Ruby Program

开发者 https://www.devze.com 2023-01-18 10:36 出处:网络
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 p

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消