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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:29:12+00:00 2026-06-11T17:29:12+00:00

I decided to experiment with defining methods inside methods in Ruby and wrote this:

  • 0

I decided to experiment with defining methods inside methods in Ruby and wrote this:

def foo
  def bar
    puts "hello"
  end
  bar
end

This defined, I ran foo and “hello” was printed as I expected. However I then tried foo.bar – which printed out “hello” twice. Why?

  • 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-06-11T17:29:14+00:00Added an answer on June 11, 2026 at 5:29 pm

    foo.bar equals foo().bar() so you call first foo which contains bar so it is executed once here, then bar is executed one more time. At the end bar is called twice.

    def foo
      puts 'foo called'
      def bar
        puts 'bar called'
        puts "hello"
      end
      bar
    end
    foo.bar
    #=> foo called
    #=> bar called
    #=> hello
    #=> bar called
    #=> hello
    

    What I don’t understand is this means the result of foo which is nil can call bar.

    >> nil.bar
    #=> hello
    

    How is that possible ?

    Edit: As explained in some answers, nested method are included in the class scope so weither bar is declared inside or outside foo does not make any difference.

    Also Matz said that it may change from Ruby2.0

    here is the example he wrote:

    class Foo
      def foo
        def bar
          #omited
        end
        bar # can be called
      end
    
      def quux
        bar # cannot be called
      end
    end
    

    edit-Ruby2.0: It finally did not get implemented. So it remains the same.

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

Sidebar

Related Questions

I decided to try to my hand at this, and have had a somewhat
I am trying to do this, I'm a full time front-end dev and am
Inspired by this topic , I decided to write a simple program that does
This question has actually came from my experiments with GWT framework but I decided
I'm having a play with HTML5 canvas, and for my first experiment I decided
I've decided for the first time to switch to InnoDB and experiment with foreign
Decided to use Apache's Common Configuration package to parse an XML File. I decided
I decided to have a go at PDO. The function below should move a
I decided to avoid for now the set preferences method for setting user settings.
I decided to take a different approach to mass assigning for security reasons and

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.