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 578667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:17:06+00:00 2026-05-13T14:17:06+00:00

I love Ruby blocks! The idea behind them is just very very neat and

  • 0

I love Ruby blocks! The idea behind them is just very very neat and convenient.

I have just looked back over my code from the past week or so, which is basically every single ruby function I ever have written, and I have noticed that not a single one of them returns a value! Instead of returning values, I always use a block to pass the data back!

I have even caught myself contemplating writing a little status class which would allow me to write code like :

something.do_stuff do |status|
  status.success do 
    # successful code
  end

  status.fail do
    # fail code
    puts status.error_message
  end
end

Am I using blocks too much? Is there a time to use blocks and a time to use return values?

Are there any gotchas to be aware of? Will my huge use of blocks come and bite me sometime?

  • 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-13T14:17:07+00:00Added an answer on May 13, 2026 at 2:17 pm

    The whole thing would be more readable as:

    if something.do_stuff
      #successful code
    else
      #unsuccessful code
    end
    

    or to use a common rails idiom:

    if @user.save
      render :action=>:show
    else
       @user.errors.each{|attr,msg| logger.info "#{attr} - #{msg}" }
       render :action=>:edit
    end
    

    IMHO, avoiding the return of a boolean value is overuse of code blocks.

    A block makes sense if . . .

    It allows code to use a resource without having to close that resource

     open("fname") do |f|
      # do stuff with the file
     end #don't have to worry about closing the file
    

    The calling code would have to do non-trivial computation with the result

    In this case, you avoid adding the return value to calling scope. This also often makes sense with multiple return values.

    something.do_stuff do |res1, res2|
       if res1.foo? and res2.bar?
          foo(res1)
       elsif res2.bar?
          bar(res2)
       end
     end #didn't add res1/res2 to the calling scope
    

    Code must be called both before and after the yield

    You see this in some of the rails helpers:

     &lt% content_tag :div do  %>
         &lt%= content_tag :span "span content" %>
      &lt% end -%>
    
    

    And of course iterators are a great use case, as they’re (considered by ruby-ists to be) prettier than for loops or list comprehensions.

    Certainly not an exhaustive list, but I recommend that you don’t just use blocks because you can.

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

Sidebar

Related Questions

I love using irb to quickly check how my ruby code reacts. Im wondering
I'd love to have a Gemfile in Bundler that just sets my own personal
I have been using ruby exclusively for about a month and I really love
I was very happy to see the google code: google-api-ruby-client project, because it meant
One thing I love about ruby is that mostly it is a very readable
I love Ruby and its framework, but I don't think that Ruby On Rails
If I want to try out something new in Ruby or Javascript, I love
I'm brand new to java, coming from a ruby world. One thing I love
I love NHibernate's ability to have one table to store multiple types based on
I love the idea of Grizzly, but I can't find any good examples to

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.