Python like printing for Ruby
When I started playing around for Ruby for the first time I was expecting some of the great convenience features of Python. In particular this:
a = [1,2]
print a # >> [1, 2]
E.g. when you print most instances you get code that would create them. This is good because it’s intuitive and usually a very concise way of expressing the contents and state of some instance.
However, when doing the same in Ruby, this happens:
a = [1,2]
print a # >> 12
It prints 12! This is not very helpful. You don’t even know it’s an array. The reason is that the default print method of an array just maps to array.join(“”) for some reason.
Anyhow, it took me a long time to figure out how this is supposed to be done in Ruby, cause I was convinced there would be some similar convenience method. Finally I foundĀ this postwhich reveals the answer:
a = [1,2]
print a.inspect # >> [1, 2]
Yey. Just what I wanted! I’m posting it here in case someone else has similar problems.

