Ruby: method to print and neat an array Ruby: method to print and neat an array arrays arrays

Ruby: method to print and neat an array


You can use the method p. Using p is actually equivalent of using puts + inspect on an object.

humans = %w( foo bar baz )p humans# => ["foo", "bar", "baz"]puts humans.inspect# => ["foo", "bar", "baz"]

But keep in mind p is more a debugging tool, it should not be used for printing records in the normal workflow.

There is also pp (pretty print), but you need to require it first.

require 'pp'pp %w( foo bar baz )

pp works better with complex objects.


As a side note, don't use explicit return

def self.print    return @@current_humans.to_s    end

should be

def self.print    @@current_humans.to_s    end

And use 2-chars indentation, not 4.