Rilcon42
Rilcon42

Reputation: 9763

ruby object to_s gives unexpected output

What is the correct way to view the output of the puts statements below? My apologies for such a simple question.... Im a little rusty on ruby. github repo

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

Upvotes: 1

Views: 50

Answers (2)

max pleaner
max pleaner

Reputation: 26768

Maybe check out the awesome_print gem.

It provides the .ai method which can be called on anything.

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).

There's also a way to return objects as HTML. It's the my_obj.ai(html: true) option.

Upvotes: 3

rootatdarkstar
rootatdarkstar

Reputation: 1526

Just use .inspect method instead of .to_s if you want to see internal properties of objects.

Upvotes: 1

Related Questions