Ruby: How to make IRB print structure for Arrays and Hashes - ruby

When I make a new array/hash in irb, it prints out a nice format to show the structure, ex.
["value1", "value2", "value3"]
{"key1" => "value1"}
... but when I try to print out my variables using puts, I get them collapsed:
value1
value2
value3
key1
value1
I gather that puts is not the right command for what I want, but what is? I want to be able to view my variables in irb in the first format, not the second.

You can either use the inspect method:
a=["value1", "value2", "value3"]
puts a.inspect
Or, even better, use the pp (pretty print) lib:
require 'pp'
a=["value1", "value2", "value3"]
pp a

Another thing you can do is use the y method which converts input into Yaml. That produces pretty nice output...
>> data = { 'dog' => 'Flemeale', 'horse' => 'Gregoire', 'cow' => 'Fleante' }
=> {"cow"=>"Fleante", "horse"=>"Gregoire", "dog"=>"Flemeale"}
>> y data
---
cow: Fleante
horse: Gregoire
dog: Flemeale

The pretty print works well, but the Awesome_Print gem is even better! You will have to require awesome_print but it handles nested hashes and arrays beautifully plus colors them in the Terminal using 'ap' instead of 'p' to puts the output.
You can also include it in your ~/.irbrc to have this as the default method for displaying objects:
require "awesome_print"
AwesomePrint.irb!

Try .inspect
>> a = ["value1", "value2", "value3"]
=> ["value1", "value2", "value3"]
>> a.inspect
=> "[\"value1\", \"value2\", \"value3\"]"
>> a = {"key1" => "value1"}
=> {"key1"=>"value1"}
>> a.inspect
=> "{\"key1\"=>\"value1\"}"
You can also use the p() method to print them:
>> p a
{"key1"=>"value1"}

My personal tool of choice for this is 'Pretty Print' and the pp method
require 'pp' # <- 'Pretty Print' Included in ruby standard library
pp({ :hello => :world, :this => ['is', 'an', 'array'] })
=> {:hello=>:world, :this=>["is", "an", "array"]}

Related

How the Ruby equivalent of Perl Data::Dumper

I am learning Ruby & Perl has this very convenient module called Data::Dumper, which allows you to recursively analyze a data structure (like hash) & allow you to print it. This is very useful while debugging. Is there some thing similar for Ruby?
Look into pp
example:
require 'pp'
x = { :a => [1,2,3, {:foo => bar}]}
pp x
there is also the inspect method which also works quite nicely
x = { :a => [1,2,3, {:foo => bar}]}
puts x.inspect

How to use Ruby's pp to format a string? [duplicate]

We know
require 'pp'
a=["value1", "value2", "value3"]
pp a
pretty prints the array as an output to the console. How do I get that pretty output into a string (a string containing the newlines that makes things pretty, etc.)?
...purpose being to return that pretty string from a method.
string_value = a.pretty_inspect
#pretty_inspect also comes along when you first require 'pp' - See: http://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/Kernel.html#method-i-pretty_inspect
If you want the version that is outputted to the irb console that is
string_value = a.inspect
and doesn't have any requires necessary.
This is a nice 'n simple way to capture the output of pp:
require 'pp'
asdf = {'a' => 1, :b => 2, 'c' => %w[ho daddy]}
foo = PP.pp(asdf, '')
puts foo
=> {"a"=>1, :b=>2, "c"=>["ho", "daddy"]}
Capturing STDOUT, which is the default channel used by puts and print and that things like pp piggyback on, is a bit more complex:
require 'pp'
require 'stringio'
asdf = {'a' => 1, :b => 2, 'c' => %w[ho daddy]}
puts 'Writing to STDOUT...'
pp asdf
# remember the old STDOUT stream...
old_stdout = $stdout
# ...and create a new stream that writes to a string.
captured_stdio = StringIO.new('', 'w')
$stdout = captured_stdio
# This is all captured...
puts 'Capturing to buffer...'
pp asdf
# reset STDOUT
$stdout = old_stdout
puts 'Capturing off...'
# show what we got...
puts captured_stdio.string
And what was printed:
Writing to STDOUT...
{"a"=>1, :b=>2, "c"=>["ho", "daddy"]}
Capturing off...
Capturing to buffer...
{"a"=>1, :b=>2, "c"=>["ho", "daddy"]}
The last two lines above were stored in captured_stdio by substituting that for the normal $stdout channel. Anything written to (what would be STDOUT) got stored. Swapping back in the original channel restored normal printing, and stopped anything else from being written to captured_stdio.
Another way to use stringio, without changing $stdout:
require 'pp'
require 'stringio'
a=["value1", "value2", "value3"]
sio = StringIO.new
PP.pp( a, sio )
puts sio.string
If you want to save the output into a string, you can use stringio
Here is an example:
#!/usr/bin/env ruby
require 'stringio'
require 'pp'
def output_to_string
sio = StringIO.new
old_stdout, $stdout = $stdout, sio
yield
$stdout = old_stdout # restore stdout
sio.string
end
result = output_to_string do
puts "hello"
pp ["value1", "value2", "value3"]
end
puts "result: #{result}"
If you exec this code you get:
result: hello
["value1", "value2", "value3"]

How do I skip headers while writing CSV?

I am writing a CSV file and CSV.dump outputs two header lines which I don't want.
I tried setting :write_headers => false but still it outputs a header:
irb> A = Struct.new(:a, :b)
=> A
irb> a = A.new(1,2)
=> #<struct A a=1, b=2>
irb> require 'csv'
=> true
irb> puts CSV.dump [a], '', :write_headers => false, :headers=>false
class,A
a=,b=
1,2
I don't think you can do it with option parameters. But you can easily accomplish what you want by not using the generate method
irb> arr = [a, a]
=> [#<struct A a=1, b=2>, #<struct A a=1, b=2>]
irb> csv_string = CSV.generate do |csv|
irb* arr.each {|a| csv << a}
irb> end
irb> puts csv_string
1,2
1,2
=> nil
I think the problem is two-fold:
CSV.dump [a]
wraps an instance of the struct a in an array, which then CSV tries to marshall. While that might be useful sometimes, when trying to generate a CSV file for consumption by some other non-Ruby app that recognizes CSV, you're going to end up with values that can't be used. Looking at the output, it isn't CSV:
class,A
a=,b=
1,2
Looking at it in IRB shows:
=> "class,A\na=,b=\n1,2\n"
which, again, isn't going to be accepted by something like a spreadsheet or database. So, another tactic is needed.
Removing the array from a doesn't help:
CSV.dump a
=> "class,Fixnum\n\n\n\n"
Heading off a different way, I looked at a standard way of generating CSV from an array:
puts a.to_a.to_csv
=> 1,2
An alternate way to create it is:
CSV.generate do |csv|
csv << a.to_a
end
=> "1,2\n"

Append a value in a hash object (in Ruby), using an already existing key?

How can I append a value in a Hash object using a key that already has a value. So for example
if I have
>> my_hash = Hash.new
>> my_hash[:my_key] = "Value1"
# then append a value, lets say "Value2" to my hash, using that same key "my_key"
# so that it can be
>> my_hash[:my_key]
=> ["Value1", "Value2"]
I know its easy to write my own method, but I just wanted to know if there is a built in method.
I don't know if I'm missing your point but have you considered the following:
1.9.3 (main):0 > h={}
=> {}
1.9.3 (main):0 > h[:key] = []
=> []
1.9.3 (main):0 > h[:key] << "value1"
=> ["value1"]
1.9.3 (main):0 > h[:key] << "value2"
=> ["value1", "value2"]
1.9.3 (main):0 > h[:key]
=> ["value1", "value2"]
The Ruby Way, 2nd Edition has a whole chapter on multi-value hashes if I recall correctly. Regardless, there's no builtin for this behaviour.
However, you can have some fun with passing a block into Hash.new.
$ irb
>> h = Hash.new { |hash, key| hash[key] = [] }
=> {}
>> h[:a] << "Value1"
=> ["Value1"]
>> h[:a] << "Value2"
=> ["Value1", "Value2"]
>> h
=> {:a=>["Value1", "Value2"]}
>>
If you want []= to always append to the value, then you'll need to monkey patch. Again, nothing built in to work that way.

Ruby equivalent of Perl Data::Dumper

I am learning Ruby & Perl has this very convenient module called Data::Dumper, which allows you to recursively analyze a data structure (like hash) & allow you to print it. This is very useful while debugging. Is there some thing similar for Ruby?
Look into pp
example:
require 'pp'
x = { :a => [1,2,3, {:foo => bar}]}
pp x
there is also the inspect method which also works quite nicely
x = { :a => [1,2,3, {:foo => bar}]}
puts x.inspect
I normally use a YAML dump if I need to quickly check something.
In irb the syntax is simply y obj_to_inspect. In a normal Ruby app, you may need to add a require 'YAML' to the file, not sure.
Here is an example in irb:
>> my_hash = {:array => [0,2,5,6], :sub_hash => {:a => 1, :b => 2}, :visible => true}
=> {:sub_hash=>{:b=>2, :a=>1}, :visible=>true, :array=>[0, 2, 5, 6]}
>> y my_hash # <----- THE IMPORTANT LINE
---
:sub_hash:
:b: 2
:a: 1
:visible: true
:array:
- 0
- 2
- 5
- 6
=> nil
>>
The final => nil just means the method didn't return anything. It has nothing to do with your data structure.
you can use Marshal, amarshal, YAML

Resources