2

I know there are several different ways of being able to combine puts statements into one. But more importantly, I'm trying to determine if there's a generally accepted/preferred style for this (I've only been able to dig up other people's clever ways of doing it, but no real reference on a preferred style).

I've seen things like this:

puts "This", "is", "fairly", "easy"  # Each word goes on it's own line

or perhaps:

puts ["This", "seems", "convoluted"].join("\n") # Each word goes on it's own line

or an "ugly" way:

  def ugly_puts
    puts "Not using quotes
And using no code indentation to preserve output formatting"
  end

or simply:

puts "This"
puts "Seems" 
puts "Straightforward."

It makes most sense for me to use the last methodology, but I'm just curious if there's a common/preferred way I should go about dealing with multiple line output like this.

sawa
  • 165,429
  • 45
  • 277
  • 381
Tony DiNitto
  • 1,244
  • 1
  • 16
  • 28

2 Answers2

4

If the lines to be printed are short enough to be put together on a single line in the source, then I would go with your first option:

puts "This", "is", "fairly", "easy"

If they are long, then I would use a heredoc:

puts <<_.unindent
  This Blah Blah ...
  Seems Blah Blah ...
  Straightforward. Blah Blah ...
_

where unindent is a method to unindent an indented heredoc along the lines suggested in Ruby indented multiline strings. Notice that in future versions of Ruby, it is likely that there will be a simpler way to unindent a heredoc, so this option will become more useful.

I see no point in using your second or fourth option. The third may be used, but it looks ugly.

Community
  • 1
  • 1
sawa
  • 165,429
  • 45
  • 277
  • 381
  • 2
    It may be helpful for others to know that `unindent` appears to be a Ruby gem that needs to be installed before this style can work. – Tony DiNitto Nov 18 '15 at 15:17
1

TL;DR preference and readability

I've scoured the following ruby style guides:

Ruby Style Guide

bbatsov's Ruby Style Guide

Neither of them mention any specific or preferred method of printing multiple puts statements.

I think it's both situational and preferential. What is more readable in the situation? If you have several long puts statements as simply strings, split them into separate puts statements on several lines.

puts "Something very longgggggggggggg"
puts "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an..."
puts  "unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing..."

If you have for some reason stored your separate statements in variables, just puts them all on a single line: puts this, that, thing.

For storing puts statements in a method, it might be convenient for times you want to print several lines out in the console. For example, when making a program where the user would interact and use with via the terminal, you might want to store certain statements within a method and on users call, print them out (i.e. Printing out instructions, or printing out available commands).

philip yoo
  • 2,462
  • 5
  • 22
  • 37