What is the difference between = and - in haml when writing ruby ​​code?

So I'm new to HAML.

When viewing the HAML tutorial, the way to represent the ruby ​​code was mentioned as =

e.g. %strong= item.title

but when I ran this code:

 = @list.documents.each do |doc| %tbody %tr %td= doc.display_name 

along with all the list data that was displayed, a lot of inactive data was also displayed that was associated with the actual data of the list that was displayed. This is what I got:

 val1 val2 val3 [# Document......@id : val1, @id:val2.....] 

When I try to use the same code when replacing = with - , unwanted data is not accepted.

 - @list.documents.each do |doc| %tbody %tr %td= doc.display_name 

output:

 val1 val2 val3 

can someone explain the difference between - and = when writing ruby ​​code in haml?

+6
source share
2 answers

They are both indicators that what follows should be treated as Ruby code. But - does not display the result for viewing, while = does.

For example, consider the following helper:

 def hlp [1,2].each(&:succ) # using example relevant to your code sample end 

each will return the enumerator to which it is called. Thus, the return value of this helper method will be [1,2] .

Below in your HAML view will not be displayed [1,2] :

 - hlp 

But this will display the following:

 = hlp 

Therefore, in the code sample you provided, you will use - , not = , because you do not want to display all @list.documents once each with them.

+6
source

In haml = used to write ruby ​​code and displays the result as output, while - used only to write ruby ​​code.
You can learn more about haml from this http://haml.info/tutorial.html

0
source

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


All Articles