Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Proper" way to output variables in a string [closed]

Tags:

ruby

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 %?

like image 887
Jeff H. Avatar asked Jan 16 '23 23:01

Jeff H.


1 Answers

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.

like image 150
Hauleth Avatar answered Jan 29 '23 08:01

Hauleth