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

  • Home
  • SEARCH
  • 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 200023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:04:30+00:00 2026-05-11T17:04:30+00:00

Saw this piece of code in a Ruby on Rails book. This first one

  • 0

Saw this piece of code in a Ruby on Rails book. This first one is from a view and the second one is a helper module. I don’t understand how that &block and the attributes={} thing work. Can anyone guide me to a tutorial of some kind explaining this?

<% hidden_div_if(@cart.items.empty?, :id => "cart") do %>
 <%= render(:partial => "cart", :object => @cart) %>
<% end %>

module StoreHelper
 def hidden_div_if(condition, attributes = {}, &block)
  if condition
   attributes["style"] = "display: none"
  end
   content_tag("div", attributes, &block)
  end
end
  • 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-05-11T17:04:31+00:00Added an answer on May 11, 2026 at 5:04 pm

    Blocks are a fairly basic part of ruby. They’re delimited by either do |arg0,arg1| ... end or { |arg0,arg1,arg2| ... }.

    They allow you to specify a callback to pass to a method.
    This callback can be invoked two ways – either by capturing
    it by specifying a final argument prefixed with &, or by
    using the yield keyword:

    irb> def meth_captures(arg, &block)
           block.call( arg, 0 ) + block.call( arg.reverse , 1 )
         end
    #=> nil
    irb> meth_captures('pony') do |word, num|
           puts "in callback! word = #{word.inspect}, num = #{num.inspect}"
           word + num.to_s
         end
    in callback! word = "pony" num = 0
    in callback! word = "ynop" num = 1
    #=> "pony0ynop1" 
    irb> def meth_yields(arg)
           yield(arg, 0) + yield(arg.upcase, 1)
         end
    #=> nil
    irb> meth_yields('frog') do |word, num|
           puts "in callback! word = #{word.inspect}, num = #{num.inspect}"
           word + num.to_s
         end
    in callback! word = "frog", num = 0
    in callback! word = "FROG", num = 1
    #=> "frog0FROG1"
    

    Note that our callback was the same in each case – we can remove
    repetition by saving our callback in an object, and then passing it to each
    method. This can be done using lambda to capture the callback in an object,
    and then passed to a method by prefixing it with &.

    irb> callback = lambda do |word, num|
           puts "in callback! word = #{word.inspect}, num = #{num.inspect}"
           word + num.to_s
         end
    #=> #<Proc:0x0052e3d8@(irb):22>
    irb> meth_captures('unicorn', &callback)
    in callback! word = "unicorn", num = 0
    in callback! word = "nrocinu", num = 1
    #=> "unicorn0nrocinu1"
    irb> meth_yields('plate', &callback)
    in callback! word = "plate", num = 0
    in callback! word = "PLATE", num = 1
    #=> "plate0PLATE1"
    

    It’s important to understand the different uses of & here as a prefix to the last argument of a function

    • in a function definition, it captures any passed block into that object
    • in a function call, it expands the given callback object into a block

    If you look around blocks are used all over the place, especially in iterators, like Array#each.

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

Sidebar

Ask A Question

Stats

  • Questions 318k
  • Answers 318k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can probably get some ideas from this different but… May 13, 2026 at 11:59 pm
  • Editorial Team
    Editorial Team added an answer The Bind method cannot be used with the Reapter control.… May 13, 2026 at 11:59 pm
  • Editorial Team
    Editorial Team added an answer You may be interested in checking out the following Google… May 13, 2026 at 11:59 pm

Related Questions

I was under the assumption that STL functions could be used only with STL
I am building a small flash application to record audio from the client's computer.
I've got a LOT of tests written for a piece of software (which is
I want to replace the standard {% if %} template tag with a smart

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.