var = 5
puts "Method #1: #{var}"
puts "Method #2: %d" % var
I’m looking for advantages and drawbacks with each method, if there are at all.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first one is simple string interpolation, while the second is syntactic sugar for Kernel#sprintf.
Use the first for cases where you just need to generate a string that includes variable data in it. The second is better for when you need to do specialized formatting beyond just putting a variable into a string. The first will have better performance, so do it that way unless you need specialized formatting.
And it case it comes up,
"hello #{var}"is much faster than"hello " + var.