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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:27:40+00:00 2026-05-16T18:27:40+00:00

I’m a beginner in Ruby, so I’m sorry to ask something so simple, but

  • 0

I’m a beginner in Ruby, so I’m sorry to ask something so simple, but is there anything wrong with this code –

3.upto(9) {
  print "Hello"
  puts " World"
}

or

3.upto(9) { |n|
  print "Hello "
  puts n
}

It works well enough, but most of the code samples I see use the syntax of

3.upto(9) do |n|
  print "Hello "
  puts n
end

is it just the convention to only use curly braces for single statements? Coming from C/C# the first seems more natural to me, but when in Rome!

  • 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-16T18:27:40+00:00Added an answer on May 16, 2026 at 6:27 pm

    There is a subtle difference between the two syntaxes. { } are higher precedence than do ... end. Thus, the following will pass bar and a block to method foo:

    foo bar do ... end
    

    while the following will pass a block to bar, and the result of that to foo:

    foo bar { ... }
    

    So your examples will act the same. However, if you left the parentheses off:

    > 3.upto 9 { 
      puts "Hi" 
    }
    SyntaxError: compile error
    (irb):82: syntax error, unexpected '{', expecting $end
    3.upto 9 { 
              ^
        from (irb):82
        from :0
    
    > 3.upto 9 do 
        puts "Hi" 
      end
    Hi
    Hi
    Hi
    Hi
    Hi
    Hi
    Hi
    => 3
    

    So, { } are more likely to catch you up if you leave off parentheses in Ruby, which is fairly common; for this reason, and because Ruby conditionals and other control constructs all use end as a delimiter, people usually use do ... end for multi-line code blocks that come at the end of a statement.

    However, { } is frequently use in places where do ... end would be cumbersome, for instance, if you are chaining several methods together which take blocks. This can allow you to write short, one line little blocks which can be used as part of a method chain.

    > [1,2,3].sort{|x,y| y<=>x}.map{|x| x+1}
    => [4, 3, 2]
    

    Here’s an example to illustrate this difference:

    def foo arg
      if block_given?
        puts "Block given to foo"
        yield arg
      else
        puts "No block given to foo"
        arg
      end
    end
    
    
    def bar
      if block_given?
        puts "Block given to bar"
        yield "Yielded from bar"
      else
        puts "No block given to bar"
      end
      "Returned from bar"
    end
    
    irb(main):077:0> foo bar { |arg| puts arg }
    Block given to bar
    Yielded from bar
    No block given to foo
    => "Returned from bar"
    irb(main):078:0> foo bar do |arg| puts arg end
    No block given to bar
    Block given to foo
    Returned from bar
    => nil
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is about as beginner as a question gets, but I can't find anything
I am beginner in Ruby and having trouble understanding this code require_relative 'custom_page' module
This seems to be basic but I'm a Ruby/Rails beginner. I need to simply
I am a beginner to ruby on rails.But i am quite comfortable with php,
I am a beginner with Ruby and Rails but I managed to use OmniAuth
I'm a beginner coder in Ruby and currently I'm trying to use this plugin:
Beginner in Android development. My code crashes. I have made a simple Java method
I'm a beginner of Ruby and Rails, so this is probably an easy question.
I'm a beginner coding a Java web application. I used to code Ruby On
I'm a beginner at Ruby on Rails so I apologize if this is quite

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.