![](/img/trans.png)
[英]Why does calling “p” on a Ruby object call to_s and then output a non string?
[英]ruby object to_s gives unexpected output
查看下面的puts
語句輸出的正確方法是什么? 對於這個簡單的問題,我深表歉意。...我對紅寶石有些生疏。 github倉庫
require 'active_support'
require 'active_support/core_ext'
require 'indicators'
my_data = Indicators::Data.new(Securities::Stock.new(:symbol => 'AAPL', :start_date => '2012-08-25', :end_date => '2012-08-30').output)
puts my_data.to_s #expected to see Open,High,Low,Close for AAPL
temp=my_data.calc(:type => :sma, :params => 3)
puts temp.to_s #expected to see an RSI value for each data point from the data above
也許看看awesome_print
gem。
它提供了可以在任何對象上調用的.ai
方法。
一個例子:
my_obj = { a: "b" }
my_obj_as_string = my_obj.ai
puts my_obj_as_string
# ... this will print
# {
# :a => "b"
# }
# except the result is colored.
您可以使用ap(my_obj)
縮短所有步驟。
還有一種將對象返回為HTML的方法。 這是my_obj.ai(html: true)
選項。
如果要查看對象的內部屬性,只需使用.inspect
方法而不是.to_s
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.