As a developer, there are many times when printing a value is essential to debugging an application. Sometimes you are unable to use the <%= string %> output block method.

Concat

The preferred method of outputting text in your views is to use the <%= “text” %> eRuby syntax. The regular puts and print methods do not operate as expected in an eRuby code block. If you absolutely must output text within a non-output code block (i.e., <% %>), you can use the concat method.

In other words the concat TextHelper method allows you to output strings within a <% code %> non-output code block. This can be helpful in debbuging by saving some time instead of writing something like this:

<%
  string = 'Q: What is a Concat?'
  choice = 2

  if choice != 2
    output = 'A cat who is a con artist'
  else
    output = "I'm pretty sure it's a country, right?"
  end
%>
<%= string + "A:" + output %>

We could instead while debugging or potentially for output reasons (though I wouldn't recommend it) use the method concat to output the string directly from the <% %> block as so.

<%
  string = 'Q: What is a Concat?'
  choice = 2

  if choice != 2
    concat 'My choice was not 2'
    output = 'A cat who is a con artist'
  else
    concat 'My choice was definitely 2!'
    output = "I'm pretty sure it's a country, right?"
  end

  concat string + 'A:' + output
%>

Although there are plenty of instances where using the concat method could be avoided via other output methods, having an additional tool for non-output blocks can sometimes come in handy.

« Previous Post
Restrict an integer to a specific range in Ruby
Next Post »
Drying out your content with Ruby on Rails Internationalization

Join the conversation

comments powered by Disqus