eLRuLL
eLRuLL

Reputation: 18799

Ruby differences between print formatting

I am starting to learn Ruby and I am using print and I saw that it can use two string formatting:

  1. print "#{var}"
  2. print "%r %d %r" % [var1,var2,var3]

Of course one difference is that in the first one you don't need to put the type of parameter you are passing, but what I really want to know is which one of this would work better for Ruby and some other tips that you could give me.

Thanks.

Upvotes: 2

Views: 404

Answers (1)

Richard Brown
Richard Brown

Reputation: 11444

Most people use the former method as it automatically calls the .to_s method of the object to display the value. You can use this to your advantage by overriding the class's .to_s method to make it display whatever you wish.

Frankly I've never seen the second method used in any code I've looked at.

Upvotes: 3

Related Questions