"# {}" designation with erb designation

What are the differences between the symbols:

" 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 really have the impression that the erb notation can be more powerful, but I'm not so clear. Can you compare them on various aspects and say which ones should be used in which cases? What things can be done in one notation and not in another?

Addition 1

Most answers so far seem to argue that erb is inefficient and should not be used when "#{ }" can be used. Now let me ask you another way. Why can't note "#{ }" replace erb? Wouldn't that be much faster and better?

Supplement 2

Most of the answers below assume that a single "#{ }" event does not extend across multiple lines, so a piece of code like a multi-line loop cannot be embedded inside. But why not? If you do this, I see no difference in doing this with <% > , except in the last you put <% > on each line.

+4
source share
3 answers

One drawback of using ERB for general string interpolation is that ERB is really aimed at creating HTML. The result will be a lot of encoding problems when you forget all the same and get confused as to why your ampersand characters are bigger, smaller, etc. Become crippled.

I think there will be more computational overhead for ERB, but that may not matter.

Also, if you put inline ERB inside Ruby, you end up with Ruby inside ERB inside Ruby (and maybe there will be more levels when calling methods from ERB), and that will be a bit confusing mess. Think of the poor souls who will need to support your code, said that the poor soul may be you.

UPDATE You can use #{} for simple HTML templates (see Mustache for something like this), but it would be difficult and rather ugly to create a repeating section; repeating sections are fairly common in HTML templates, so this should be easy and natural for the HTML template system. With ERB, you can simply:

 <% a.each do |e| %> <!-- ... --> <% end %> 

but it will be a mess in the interpolation #{} . You would also end up making #{html(blahblah)} all the time, and most people will forget.

In addition, โ€œ#โ€ has special meaning in URLs and another special meaning in CSS (where curly braces also have special meaning); URL snippets and CSS snippets are pretty common in HTML templates, so you'll always worry about mixing things. This particular nastiness bit is likely to be a problem only when using #{} to create a CSS selector or fragment in a URL, but this is enough to make it cumbersome.

+1
source

You are right that your version of ERB should give the same result as the regular version (I think), but its purpose is completely different.

ERB really only suitable for templates: if you need to generate text (for example, an HTML page or a document), then a template engine such as ERB will be a suitable tool. However, this does not mean simple string interpolation: although it certainly can do it, itโ€™s a pretty tweety way to get around this. Indeed, you actually create an ERB object from a string, and then evaluate it back to the string.

You can get an idea of โ€‹โ€‹how inefficient this is with a quick reference:

 $ 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 uses #{} inside a string literal. It is built at the language level (as opposed to the library level).

+5
source

Benefits for ERB:

  • Everyone already knows and "loves" this style, similar to PHP
  • You donโ€™t need to worry about balancing or accelerating ' or "
  • Usually you do not need to program the basic Ruby engine, i.e. you donโ€™t need to define a method, possibly located in a class, perhaps with a module, etc.

In the big picture, I think the general idea is to separate the parts of the program that should be done by the software developer from the part that the web developer can do. For example, a software developer may simply indicate that @account_number is available and may be referenced by HTML servers without Ruby qualifications.

In real life, there seem to be many projects with one person, but at least once it was the norm to drop a team on a typical problem. Even today, take a look at all of the Rails questions here on SO from developers who obviously don't know much about Ruby. And ERB precedes Rails, so if you go back to the origins of the mechanism, there was a time when you really always needed a real software department, because every project included building a framework, so you probably didn't want to require every last member of the team from Ruby .

Now I understand what you mean. If you already know Ruby, itโ€™s much harder to justify the ugliness of erb.

+4
source

Source: https://habr.com/ru/post/1347488/


All Articles