![](/img/trans.png)
[英]For Rails, how to access or print out config variables (as experiment or test / debugging)
[英]How do I print out the contents of an object in Rails for easy debugging?
我想我正在嘗試獲得print_r()
的 PHP 等效項(打印人類可讀); 目前的原始輸出是:
ActiveRecord::Relation:0x10355d1c0
我應該怎么辦?
我通常首先嘗試.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
希望有幫助。
在模型中定義 to_s 方法。 例如
class Person < ActiveRecord::Base
def to_s
"Name:#{self.name} Age:#{self.age} Weight: #{self.weight}"
end
end
然后,當您使用 #puts 打印它時,它將顯示帶有這些變量的字符串。
在 Rails 中,您可以使用調試助手ActionView::Helpers::DebugHelper在視圖中打印結果
# app/view/controllers/post_controller.rb
def index
@posts = Post.all
end
#app/view/posts/index.html.erb
<%= debug(@posts) %>
#start your server
rails s
結果(在瀏覽器中)
- !ruby/object:Post
raw_attributes:
id: 2
title: My Second Post
body: Welcome! This is another example post
published_at: '2015-10-19 23:00:43.469520'
created_at: '2015-10-20 00:00:43.470739'
updated_at: '2015-10-20 00:00:43.470739'
attributes: !ruby/object:ActiveRecord::AttributeSet
attributes: !ruby/object:ActiveRecord::LazyAttributeHash
types: &5
id: &2 !ruby/object:ActiveRecord::Type::Integer
precision:
scale:
limit:
range: !ruby/range
begin: -2147483648
end: 2147483648
excl: true
title: &3 !ruby/object:ActiveRecord::Type::String
precision:
scale:
limit:
body: &4 !ruby/object:ActiveRecord::Type::Text
precision:
scale:
limit:
published_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
subtype: &1 !ruby/object:ActiveRecord::Type::DateTime
precision:
scale:
limit:
created_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
subtype: *1
updated_at: !ruby/object:ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter
subtype: *1
pp 也可以完成這項工作,不需要任何寶石。
@a = Accrual.first ; pp @a
#<Accrual:0x007ff521e5ba50
id: 4,
year: 2018,
Jan: #<BigDecimal:7ff521e58f08,'0.11E2',9(27)>,
Feb: #<BigDecimal:7ff521e585d0,'0.88E2',9(27)>,
Mar: #<BigDecimal:7ff521e58030,'0.0',9(27)>,
Apr: #<BigDecimal:7ff521e53698,'0.88E2',9(27)>,
May: #<BigDecimal:7ff521e52fb8,'0.8E1',9(27)>,
June: #<BigDecimal:7ff521e52900,'0.8E1',9(27)>,
July: #<BigDecimal:7ff521e51ff0,'0.8E1',9(27)>,
Aug: #<BigDecimal:7ff521e51bb8,'0.88E2',9(27)>,
Sep: #<BigDecimal:7ff521e512f8,'0.88E2',9(27)>,
Oct: #<BigDecimal:7ff521e506c8,'0.0',9(27)>,
Nov: #<BigDecimal:7ff521e43d38,'0.888E3',9(27)>,
Dec: #<BigDecimal:7ff521e43478,'0.0',9(27)>,
您還可以打印對象的兩個實例:
pp( Accrual.first , Accrual.second)
`
`
`
inspect
很棒,但有時還不夠好。 例如BigDecimal
打印如下: #<BigDecimal:7ff49f5478b0,'0.1E2',9(18)>
。
要完全控制打印的內容,您可以重新定義to_s
或inspect
方法。 或者創建自己的一個,以免讓未來的開發人員感到困惑。
class Something < ApplicationRecord
def to_s
attributes.map{ |k, v| { k => v.to_s } }.inject(:merge)
end
end
這將對所有屬性應用一個方法(即to_s
)。 這個例子將擺脫丑陋的BigDecimals
。
您還可以僅重新定義少數屬性:
def to_s
attributes.merge({ my_attribute: my_attribute.to_s })
end
您還可以創建兩者的混合或以某種方式添加關聯。
.inspect
是您正在尋找的,它比 IMO 更容易.to_yaml
!
user = User.new
user.name = "will"
user.email = "will@example.com"
user.inspect
#<name: "will", email: "will@example.com">
instance.attributes
在 rails 控制台打印整個哈希。
您需要使用debug(@var)
。 它與“print_r”完全一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.