2

For the following code, which according to the style guide should be wrapped at 80 chars:

opts.on('--scores_min <uint>', Integer, 'Drop reads if a single position in ',
                                        'the index have a quality score ',
                                        'below scores_main (default= ',
                                        "#{DEFAULT_SCORE_MIN})") do |o|
  options[:scores_min] = o
end

The resulting output is:

    --scores_min <uint>          Drop reads if a single position in
                                 the index have a quality score
                                 below scores_main (default=
                                 16)

Which wraps at 72 chars and looks wrong :o(

I really want it wrapped at 80 chars and aligned like this:

    --scores_min <uint>          Drop reads if a single position in the
                                 index have a quality score below
                                 scores_min (default=16)

How can this be achieved in a clever way?

1
  • 1
    Much of coding is programmer's choice. The style guides are guidelines, not rules. I recommend looking at the code with a critical eye towards readability, and if you're still not sure, have a peer look at it. And read the style guides as they are great for showing us the issues and how to think about writing code for readability. Apr 9, 2016 at 22:27

2 Answers 2

1

The easiest solution in this case is to stack parameters like this:

opts.on('--scores_min <uint>',
        Integer, 
        "Drop reads if a single position in the ",
        "index have a quality score below ",
        "scores_min (default= #{DEFAULT_SCORE_MIN})") do |o|
  options[:scores_min] = o
end

That results in a fairly pleasant output:

    --scores_min <uint>          Drop reads if a single position in the 
                                 index have a quality score below 
                                 scores_min (default= 16)

More generally, here docs can make it easier to format output strings in a way that looks good both in the code and in the output:

       # Deeply nested code
       puts <<~EOT
            Drop reads if a single position in the 
            index have a quality score below 
            scores_min (default= #{DEFAULT_SCORE_MIN})
       EOT

But in this case it doesn't work so well since the description string is indented automatically.

0

So I think the solution is to follow the Ruby Style Guide:

When using heredocs for multi-line strings keep in mind the fact that they preserve leading whitespace. It's a good practice to employ some margin based on which to trim the excessive whitespace.

code = <<-END.gsub(/^\s+\|/, '')
  |def test
  |  some_method
  |  other_method
  |end
END
# => "def test\n  some_method\n  other_method\nend\n"

[EDIT] In Ruby 2.3 you can do (same ref):

code = <<~END
  def test
    some_method
    other_method
  end
END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.