简体   繁体   English

红宝石对象to_s提供了意外的输出

[英]ruby object to_s gives unexpected output

What is the correct way to view the output of the puts statements below? 查看下面的puts语句输出的正确方法是什么? My apologies for such a simple question.... Im a little rusty on ruby. 对于这个简单的问题,我深表歉意。...我对红宝石有些生疏。 github repo 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

Maybe check out the awesome_print gem. 也许看看awesome_print gem。

It provides the .ai method which can be called on anything. 它提供了可以在任何对象上调用的.ai方法。

An example: 一个例子:

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. 

You can shorten all this into a single step with ap(my_obj) . 您可以使用ap(my_obj)缩短所有步骤。

There's also a way to return objects as HTML. 还有一种将对象返回为HTML的方法。 It's the my_obj.ai(html: true) option. 这是my_obj.ai(html: true)选项。

如果要查看对象的内部属性,只需使用.inspect方法而不是.to_s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM