I have seen quite a few ways to output variables. Which of these ways is "better"?
my_name = 'Jeff Hall'
my_height = 74
puts "Let's talk about %s." % my_name
puts "He's %d inches tall." % my_height
vs.
puts "Let's talk about #{my_name}."
puts "He's #{my_height} inches tall."
Also, what do the s
and d
represent after the %
?
I prefer the second way unless I need to format string. For more questions about style check GitHub Styleguide for Ruby.
About %s
and %d
. They are formatters, list of them is on Ruby Kernel::sprintf doc page.
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