Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to dynamically generate some MarkDown formatted documents, with header fields of arbitrary lengths.

I want to have a title of arbitrary length underlined like this

This should work to

The code I came up with to do this in my ERB views.

<%= @title %>
<%={"-"}.join %>

Is there a more efficient way to make this happen?

share|improve this question
up vote 3 down vote accepted

One way is to use multiplication. Multiplying a String by a Fixnum will produce a new String where the original String appears the Fixnum of times.

"-" * @title.length #=> "---------------" 

Documentation for String#*

Warning: Make sure you put the String first or else you will get a TypeError: String can't be coerced into Fixnum since that would be calling Fixnum#* instead of String#*.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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