我想我正在尝试获得print_r()
(人类可读的印刷体)的PHP等价物;目前的原始输出是:
ActiveRecord::Relation:0x10355d1c0
我该怎么办?
发布于 2011-01-28 23:43:42
我通常首先尝试.inspect
,如果它不能满足我的需求,我将切换到.to_yaml
。
class User
attr_accessor :name, :age
end
user = User.new
user.name = "John Smith"
user.age = 30
puts user.inspect
#=> #<User:0x423270c @name="John Smith", @age=30>
puts user.to_yaml
#=> --- !ruby/object:User
#=> age: 30
#=> name: John Smith
希望这能有所帮助。
发布于 2011-01-28 23:33:01
在模型中定义to_s方法。例如
class Person < ActiveRecord::Base
def to_s
"Name:#{self.name} Age:#{self.age} Weight: #{self.weight}"
end
end
然后,当您使用#put打印它时,它将显示包含这些变量的字符串。
发布于 2014-02-04 22:52:39
https://stackoverflow.com/questions/4829909
复制相似问题