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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:37:47+00:00 2026-05-29T03:37:47+00:00

For example def test a = a is for apple def inner_method a =

  • 0

For example

def test
    a = "a is for apple"
    def inner_method
        a = "something" # this will refer to a different "a"
    end

    inner_method
    puts a
end

Are there any reasons for this? Blocks have lexical scope, so why don’t methods? Is this going to be fixed?

  • 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-29T03:37:48+00:00Added an answer on May 29, 2026 at 3:37 am

    It’s because Ruby’s methods aren’t first class objects (as they would be in IO, for example). So when you define the inner method, what is the receiver? Presumably the method itself, or the binding or something, but Ruby doesn’t have that deep of OO.

    Anyway, it’s unclear to me what you were expecting to happen in your example, were you wanting it to modify the local variable a? If so, a proc is a suitable substitute for a method.

    def test
      a = "a is for apple"
      inner_method = lambda do
        a = "something"
      end
    
      a # => "a is for apple"
      inner_method.call
      a # => "something"
    end
    
    test
    

    "functional.rb" is a more extravagant example of this style of programming.

    And "lambda, proc, and Proc.new" is a breakdown of Ruby’s different types of closures.

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

Sidebar

Related Questions

Possible Duplicate: 'pass parameter by reference' in Ruby? in this example: def test verb
For example we have: module X def test X end end module Y def
Let there be class Example defined as: class Example def initialize(test='hey') self.class.send(:define_method, :say_hello, lambda
For example, The function could be something like def RandABCD(n, .25, .34, .25, .25):
I will go straight to the example: class Foo: @execonce def initialize(self): print 'Called'
Say I want to test this module: import osutils def check_ip6(xml): ib_output = osutils.call('iconfig
I'm testing exceptions with nose. Here's an example: def testDeleteUserUserNotFound(self): Test exception is raised
Imagine I have a test like this: class MyUnitTest < Test::Unit::TestCase def test_first #
I'm making unit tests to a model. This test RUN ok def test_event_creation(self): new_user
I have the following code (as an example) in a unittest TestCase def test(self):

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.