What Is the Meaning of Erb

Where is Ruby's ERB format officially defined?

erb was developed by Masatoshi Seki as a Ruby implementation of eRuby, so its specification almost follows that of eRuby. One difference the author mentions is:

% cat hello.erb
Hello, <% print "World" %>.

% eruby hello.erb
Hello, World.

% erb hello.erb
WorldHello, .

in which case you can do:

% cat hello2.erb
Hello, <%= "World" %>.

% eruby hello2.erb
Hello, World.

% erb hello2.erb
Hello, World.

to let them work the same way.

Here is an explanation about it by the author, and here and here are documentation written by the author.

What is the meaning of - in blocks of server-side code in ruby on rails?

Adding the "-" to the end of the tag removes the line break for that line, and any whitespace characters that may follow. Likewise, adding it to the beginning removes any whitespace characters that may precede it.

For instance,

Some text.
<% -%>
More text.

results in:

Some text.
More text. # The linebreak in line 2 was suppressed in the output.

What is the difference between %, %=, %# and -% in ERB in Rails?

<% %>

Executes the ruby code within the brackets.

<%= %>

Prints something into erb file.

<%== %>

Equivalent to <%= raw %>. Prints something verbatim (i.e. w/o escaping) into erb file. (Taken from Ruby on Rails Guides.)

<% -%>

Avoids line break after expression.

<%# %>

Comments out code within brackets; not sent to client (as opposed to HTML comments).

Visit Ruby Doc for more infos about ERB.

What is the meaning of %%= in Ruby on Rails?

In short, ERb processes double-percent marks into single-percent marks.


It looks like you're using one layer of ERb templates to generate another layer of ERb templates.

The first layer of ERb doesn't need a variable called content, just the t method:

<span class="option-content" placeholder="<%=t('pages.edit.option')%>">
<%%= content %>
</span>

That first layer is rendered to produce the second layer:

<span class="option-content" placeholder="Edit">
<%= content %>
</span>

As you can see, that is also an ERb template. I expect that something else, later on, takes that second ERb and uses it to render something like:

<span class="option-content" placeholder="Edit">
Hello, world.
</span>

What does -% mean in Ruby on Rails, compared to %

<%= some_code -%> The minus at the end removes the newline. Useful for formatting the generated HTML, while <%= some_code %> does not.

Thanks, Anubhaw

erb defining a min and maximum

You can use Range#include?

[2] pry(main)> (1..9).include? 2
=> true

And in your case

<% if (1..9).include?(@groups[location].size) %>

I'd prefer two conditions probably:

<% if @groups[location].size >= 1 && @groups[location].size <= 9 %>

EDIT:

Just as Mr. Sergio predicted I'd prefer Comparable#between?:

<% @groups[location].size.between?(1,9) %>

Range#cover? is quite good as well:

<% if (1..9).cover?(@groups[location].size) %>

Worth reading: What is the difference between `Range#include?` and `Range#cover?`?

What is the meaning of %%= in Ruby on Rails?

In short, ERb processes double-percent marks into single-percent marks.


It looks like you're using one layer of ERb templates to generate another layer of ERb templates.

The first layer of ERb doesn't need a variable called content, just the t method:

<span class="option-content" placeholder="<%=t('pages.edit.option')%>">
<%%= content %>
</span>

That first layer is rendered to produce the second layer:

<span class="option-content" placeholder="Edit">
<%= content %>
</span>

As you can see, that is also an ERb template. I expect that something else, later on, takes that second ERb and uses it to render something like:

<span class="option-content" placeholder="Edit">
Hello, world.
</span>


Related Topics



Leave a reply



Submit