मुझे लगता है कि मैं PHP को print_r()(मानव-पठनीय प्रिंट करने के बराबर) प्राप्त करने की कोशिश कर रहा हूं ; वर्तमान में कच्चा माल है:
ActiveRecord::Relation:0x10355d1c0
मुझे क्या करना चाहिए?
मुझे लगता है कि मैं PHP को print_r()(मानव-पठनीय प्रिंट करने के बराबर) प्राप्त करने की कोशिश कर रहा हूं ; वर्तमान में कच्चा माल है:
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
उम्मीद है की वो मदद करदे।
y record_name.attributes। #yके लिए एक उपनाम है to_yaml।
अपने मॉडल में to_s विधि को परिभाषित करें। उदाहरण के लिए
class Person < ActiveRecord::Base
def to_s
"Name:#{self.name} Age:#{self.age} Weight: #{self.weight}"
end
end
फिर जब आप इसे #puts के साथ प्रिंट करने जाएंगे तो यह उस स्ट्रिंग को उन चर के साथ प्रदर्शित करेगा।
puts my_model_instanceफोन नहीं करेगा to_s। आपको स्पष्ट रूप से ऐसा करना होगा:puts my_model_instance.to_s
रेल में आप डिबग 'हेल्पर एक्शन व्यू :: हेल्पर्स :: डीबग हेल्पर का उपयोग करके परिणाम को प्रिंट कर सकते हैं
#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
.inspectक्या आप के लिए देख रहे हैं, यह रास्ता आसान IMO से है .to_yaml!
user = User.new
user.name = "will"
user.email = "will@example.com"
user.inspect
#<name: "will", email: "will@example.com">
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
आप दो का मिश्रण भी बना सकते हैं या किसी तरह जुड़ाव बना सकते हैं।
पीपी भी काम करता है, कोई मणि की आवश्यकता नहीं है।
@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)
`
`
`
आपको उपयोग करने की आवश्यकता है debug(@var)। यह बिल्कुल "print_r" जैसा है।