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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:36:24+00:00 2026-05-31T15:36:24+00:00

I have two examples that give the same result. With block: def self.do_something(object_id) self.with_params(object_id)

  • 0

I have two examples that give the same result.

With block:

def self.do_something(object_id)
  self.with_params(object_id) do |params|
    some_stuff(params)
  end
end

def self.with_params(object_id, &block)
  find_object_by_id
  calculate_params_hash
  block.call(params_hash)
end

and with method:

def self.do_something(object_id)
  some_stuff(self.get_params(object_id))
end

def self.get_params(object_id)
  find_object_by_id
  calculate_params_hash
  params_hash
end

The second solution seems more straightforward, but I found some usages of the first one in our application code. My question is: in which situation the first one is recommended? What are the pros and cons of each one?

  • 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-31T15:36:25+00:00Added an answer on May 31, 2026 at 3:36 pm

    The main difference between a block and function as per your example is that the block runs within the context of the calling function.

    So if your example was as:

    def self.do_something(object_id)
      x = "boogy on"
      self.with_params(object_id) do |params|
        some_stuff(params)
        puts x
      end
    end
    

    The code within the block can access the variable x that was defined outside the block. This is called a closure. You couldn’t do this if you were just calling a function as per your second example.

    Another interesting thing about blocks is they can affect the control flow of the outer function. So it is possible to do :

    def self.do_something(object_id)
      self.with_params(object_id) do |params|
        if some_stuff(params)
            return
        end
      end
    
      # This wont get printed if some_stuff returns true.
      puts "porkleworkle"
    end
    

    If the some_stuff call within the block returns a true value, the block will return. This will return out of the block and out of the dosomething method. porkleworkle would not get output.

    In your examples you don’t rely on either of these, so using function calls is probably much cleaner.

    However, there are many situations where using blocks to allow you to take advantage of these things is invaluable.

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

Sidebar

Related Questions

Will two URLs that both give the same etag have their representations treated as
I have two examples I have a question about. Let me explain via some
Example: I have two shared objects (same should apply to .dlls). The first shared
Given that I have two lists that each contain a separate subset of a
I have two lists [a,b,c,d] and [b,d,a,c] How can I make a function that
Just to give you an example: I have a PHP script that manages users
I have two websites: http://example.com http://example.com/sub I have a page, let's say: http://example.com/sub/page1 Is
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two virtual hosts on windows(for example: test1.dev and test2.dev). But it always
I have two tables, for example: Table A Table B ======= ======= Name |

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.