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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:42:35+00:00 2026-05-25T21:42:35+00:00

I come from a java/c++ background and I just started learning Ruby. I am

  • 0

I come from a java/c++ background and I just started learning Ruby. I am having problems understanding blocks attached to methods. This is a method used to migrate a database.

create_table :model_names do |t|
    t.string :name
    t.string :address
    t.timestamps
end

My question is: when I use the rake db:migrate command. Does that call the create_table method and pass a TableDefinition object to it, which I grab as |t| and set that object attributes in my block?

  • 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-25T21:42:36+00:00Added an answer on May 25, 2026 at 9:42 pm

    When you pass a block to a method, as is the case in the example here, it is up to the method to decide how and if that block is used. You will need to read the documentation and/or source code of the method to understand what parameters your block will need, if any.

    In the case of create_table, an object is prepared and passed to you by the create_table method itself. rake and the associated task have nothing to do with it at this point, they are only used as a launch mechanism.

    It’s important to keep in mind that Ruby blocks can be called zero or more times, either immediately or in the future. This means you can’t be sure if your block will be called right away, later on, or never, or how many times it will be called. The only expectation you can have is established by the method you send the block to.

    You can even pass blocks to methods that don’t want them where nothing will happen since that method never exercises your block with a yield.

    Blocks can be a bit confusing at first unless you come from a language that has a similar construct. JavaScript programmers will be familiar with passing in function objects, which is basically all you’re doing here, though in Ruby terms it’s a Proc that’s being sent in as an implicit argument.

    In a more JavaScript flavored example, this would look like:

    create_table('model_names', function(t) {
      t.string('name');
      t.string('address');
      t.timestamps();
    });
    

    Spelled out like this it’s obvious you’re simply sending in a function, and it is up to the create_table function to execute it. Ruby is structured in such a way that at a glance it might seem like that block gets executed right away, but there is a big difference between declaring a block with do ... end and begin ... end.

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

Sidebar

Related Questions

I am new to C++ (just starting). I come from a Java background and
I come from a java background, I know how to call methods from objects
I come from an Eclipse/Vim background, and I've just started using Emacs for the
I’m relatively new to working with Scala having recently come from a java background.
I am using C# having come from a Java background - I have an
I come from Java Background and so used to Debugging using Eclipse but have
I come from a Java background, where packages are used, not namespaces. I'm used
I come from a Java background and with any servlets-based technology, it's trivial to
I come from a java background. But I would like a cross-platform perspective on
I come from a Java background and have been using C# for the last

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.