Ruby Object ID方法不起作用

时间:2016-03-31 13:39:42

标签: ruby

我正在浏览www.ruby-doc的Classes and Objects教程,其中包含以下代码:

person = "Tim"
person.id   »   537771100
person.type »   String
person      »   "Tim"

我尝试了以下代码:

person = "Tim"
person.id
# => NoMethodError: undefined method `id' for "Tim":String

为什么我在新变量上调用id后会返回错误?

1 个答案:

答案 0 :(得分:3)

在最近的红宝石中,方法是object_idclass

person = "Tim"
person.object_id # => 66765660 (varies)
person.class     # => String