Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"#{ }" notation vs. erb notation

What are the differences between the notation:

"
  some text
  #{some_ruby_code}
  some text
  #{some_more_ruby_code}
  some_other_text
"

and

ERB.new("
  some text
  <%=some_ruby_code%>
  some text
  <%=some_more_ruby_code%>
  some_other_text
").result

I just have a broad impression that the erb notation may be more powerful, but am not so clear. Can you compare them from various aspects, and tell which should be used in what kind of occasions? What kinds of things can be done in one notation and not in the other?

Addition 1

Most of the answers so far seem to be claiming how erb is inefficient, and shouldn't be used when "#{ }" can be used. Now, let's ask the other way. Why can't the "#{ }" notation replace erb? Wouldn't it be much faster and better?

Addition 2

Most answers below seem to be assuming that a single occurance of "#{ }" does not span over multiple lines, so that a chunk of code like a loop taking multiple lines cannot be embedded within. But why not? If you do so, I don't see any difference from doing it with <% >, except that in the latter you put <% > in every line.

like image 644
sawa Avatar asked Dec 28 '22 22:12

sawa


2 Answers

You're right that your ERB version should produce the same result as the normal version (I think), but its purpose is entirely different.

ERB is really just for templating: if you need to generate text (like an HTML page, or a document), some templating engine like ERB will be the right tool. It's not meant for simple string interpolation, though: while it certainly is capable of doing that, it's a rather periphrastic way of going about it. Indeed, you're actually creating an ERB object from a string, and then evaluating it back into a string.

You can get an idea of how very inefficient this is with a quick benchmark:

$ irb -rbenchmark -rerb
ruby-1.9.2-p136 :023 > Benchmark.bm do |bm|
ruby-1.9.2-p136 :024 >     bm.report 'interpolation' do
ruby-1.9.2-p136 :025 >       a = 'hello there'
ruby-1.9.2-p136 :026?>     5000.times { "well #{a}" }
ruby-1.9.2-p136 :027?>     end
ruby-1.9.2-p136 :028?>   bm.report 'erb' do
ruby-1.9.2-p136 :029 >       a = 'hello there'
ruby-1.9.2-p136 :030?>     5000.times { ERB.new("well <%= a %>").result(binding) }
ruby-1.9.2-p136 :031?>     end
ruby-1.9.2-p136 :032?>   end
      user     system      total        real
interpolation  0.000000   0.000000   0.000000 (  0.001495)
erb  0.340000   0.000000   0.340000 (  0.352098)
 => true 

The standard way to interpolate Ruby code into strings is using #{} within a string literal. This is built in at the language level (as opposed to at the library level).

like image 98
Jonathan Sterling Avatar answered Jan 13 '23 00:01

Jonathan Sterling


Advantages to ERB:

  1. Everyone already knows and "loves" that PHP-like style
  2. You don't need to worry about balancing or escaping ' or "
  3. Typically you don't need to program the underlying Ruby mechanism, i.e., you don't need to define a method, possibly located in a class, perhaps qualified with a module, etc...

In the big picture, I believe the overall idea is to separate out the parts of a program that need to be done by a software developer from the part that a front-end web developer can do. For example, the software developer can simply specify that @account_number is available and it can be referenced by HTMLers without Ruby qualifications.

In real life, there seem to be a lot of one-person projects, but at least at one time the norm was to throw a team at a typical problem. Even today, look at all the Rails questions here on SO from developers that obviously know hardly any Ruby. And ERB predates Rails, so if you go back to the origins of the mechanism there was a time you really always needed a real software department because every project involved putting together a framework, so you probably didn't want to require Ruby software qualifications from every last member on the team.

Now, I do see what you mean. If you already know Ruby it's a lot harder to justify the erb ugliness.

like image 40
DigitalRoss Avatar answered Jan 13 '23 00:01

DigitalRoss