Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8360543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:29:37+00:00 2026-06-09T11:29:37+00:00

The code below is working -@items.each do |item| %tr %td =item.quantity times %td= item.product.title

  • 0

The code below is working

-@items.each do |item|
 %tr
  %td
   =item.quantity
   times
  %td= item.product.title
 end

However I want to use { } instead of do ... end. Is it possible?

-@items.each { |item|
 %tr
  %td
   =item.quantity
   times
  %td= item.product.title
 }

For some reason it throws an exception. What did I do wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:29:39+00:00Added an answer on June 9, 2026 at 11:29 am

    In your working example the end line is being treated as raw text, and will be included in your output. This works because you effectively don’t have an end keyword. If you change this line to - end — i.e. add the hyphen (and make sure you’ve got your indentation correct) — you’ll see an error like:

    Syntax error on line 9: You don't need to use "- end" in Haml. Un-indent to close a block:
    - if foo?
      %strong Foo!
    - else
      Not foo.
    %p This line is un-indented, so it isn't part of the "if" block
    

    Note that Haml doesn’t always disallow end keywords, only end by itself. You can use them with statement modifiers, e.g.:

    - @things.each do |thing|
      %p= thing.to_s
    -end unless @dont_show_things
    

    or if you want to make use of the result of the block:

    = @things.map do |thing|
      - do_something_with(thing)
    -end.join(",")
    

    As for using {}, you can’t. When Haml compiles the template to Ruby the hardcoded string end is added to close any blocks as needed. It could be possible to modify Haml to be able to use do..end or {}, but the extra complexity probably isn’t worth it. This way is a better fit with the common Ruby idiom of using do...end for multiline blocks, and {} for single line ones.

    If you need to use {} because of some issue with operator precedence it might be a sign to create a helper method. Your Haml templates (or templates in any language) aren’t the best place for complex logic, it’s best to move it into real Ruby files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below is working nearly flawlessly, however my value for page title on
I want to display some text below each grid image. Please see my code
The code below working for only first div. I can't display other divs when
Code below is working well as long as I have class ClassSameAssembly in same
The code below is working, but with a problem I do not understand. When
The code below is working. It will progress through all columns in the sheet
Technology Used:- Asp.Net 2.0 Code:- See Below Description:- hello code given below is working
The below code is working great for handling errors for text fields in my
the below code is working fine if the user enters the USN number that
I have a working JavaScript code below which dynamically creates JSON object using JSON.parse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.