我正在浏览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
后会返回错误?
答案 0 :(得分:3)
在最近的红宝石中,方法是object_id
和class
:
person = "Tim"
person.object_id # => 66765660 (varies)
person.class # => String