It's pretty wierd, but I do not know what to configure or where to configure. I am trying to print a simple hash value as below:
#!/usr/bin/ruby
names = Hash.new
names[1] = "Jane"
names[2] = "Thomas"
puts names
I expect the output to be
{1=>"Jane", 2=>"Thomas"}
while I get
1Jane2Thomas
Any ideas?
You should use inspect.
puts names.inspect
#=> {1=>"Jane", 2=>"Thomas"}
The puts method calls to_s on its argument(s) and prints the result. The p method however calls inspect on its argument(s) and prints the result:
{1=>"Jane", 2=>"Thomas"}.to_s
#=> '1Jane2Thomas'
{1=>"Jane", 2=>"Thomas"}.inspect
#=> '{1=>"Jane", 2=>"Thomas"}'
So, to have a nice Hash printout, either use
puts {1=>"Jane", 2=>"Thomas"}.inspect
or
p {1=>"Jane", 2=>"Thomas"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With