当对象显示在 Ruby 控制台上时调用的方法
我愿意在尝试显示对象时调用一个方法,但我没有找到使用哪个方法,例如:
[41] pry(main)> u
=> {"id"=>3}
[42] pry(main)> u.inspect
=> "#<User id=3>"
[43] pry(main)> u.to_s
=> "#<User id=3>"
[44] pry(main)> puts c
#<User id=3>
=> nil
我想知道第一种情况调用了哪个方法。
User 不是 ActiveRecord 类,它继承自 Hashie。
感谢您的帮助!
I am willing to call a method when trying to display an object, but I don't find which method is used, for example:
[41] pry(main)> u
=> {"id"=>3}
[42] pry(main)> u.inspect
=> "#<User id=3>"
[43] pry(main)> u.to_s
=> "#<User id=3>"
[44] pry(main)> puts c
#<User id=3>
=> nil
I would like to know which method is called for the first case.
User is not an ActiveRecord class, it inherits from Hashie.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是 Rails 方法属性 - 尽管它是 Pry 的一个功能,而不是标准 Rails 控制台的功能,后者会给您 ruby-1.9.2-p290 :047 >你
=> #
It's probably the rails method
attributes
- though it is a feature of Pry, not of the standard rails console which would give youruby-1.9.2-p290 :047 > u
=> #<u:0x8a2f6cc @id=3>