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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:03:12+00:00 2026-05-24T18:03:12+00:00

I came across an odd bug in my code that revealed an interesting behavior

  • 0

I came across an odd bug in my code that revealed an interesting behavior of ruby. Hopefully someone can explain why it behaves this way.

I had a class with an instance variable @foo and a method that referenced a locally scoped variable foo. I refactored part of the method out accidentally leaving the reference to foo; the variable no longer defined in the scope. It ended up pointing to @foo instead. Changing foo made changes to @foo and vice versa.

Simplified version:
EDIT : added ImOutOfNames.

class ImOutOfNames
    attr_accessor :foo # the culprit!
end

class Bar < ImOutOfNames
    def initialize
        @foo = "that is a tasty burger"
    end

    def bar_method_1
        foo = "Come on Yolanda, whats Fonzie like?"
        bar_method_2
    end

    def bar_method_2
        puts foo
    end
end

And the output of bar_method_1 and bar_method_2 was “that is a tasty burger”. I was expecting there to be an error, for example running the above code gets

NameError: undefined local variable or method

I even had a more senior developer come take a look and he was somewhat baffled and confirmed the behavior.

Is this an expected behavior and I misunderstood how @variables work or is there something wrong?

  • 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-24T18:03:13+00:00Added an answer on May 24, 2026 at 6:03 pm

    Your previous buggy code probably at an attr_accessor definition that created a method foo that accessed to your instance variable,

    You can have the same behavior if your code is like this:

    class Bar
    
      attr_accessor :foo
    
      def initialize
        @foo = "that is a tasty burger"
      end
    
      def bar_method_1
        foo = "Come on Yolanda, whats Fonzie like?"
        bar_method_2
      end
    
      def bar_method_2
        puts foo
      end
    end
    

    The attr_accessor call defines two methods in your object;

    def foo
      @foo
    end
    
    def foo=(value)
      @foo = value
    end
    

    So in your case, when no local variable was defined, the method was used, but as you did not call attr_accessor in this example you posted the method was not defined and there was no local variable to use on bar_method_2 so the call fails.

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

Sidebar

Related Questions

I just came across an odd method in some Ruby code, and I'm wondering
I recently came across some odd behavior that involves Java's regex engine. When writing
I came across a controller in an older set of code (Rails 1.2.3) that
So I'm just learning C#, and came across something that I find odd... I'm
Came across code recently in Wicket like so: Form<?> form = new Form<Void>(form) Can
Came across the term pigeon computing while reading about how google works.. Can someone
Came across this error today. Wondering if anyone can tell me what it means:
I came across a printed article by Bertrand Meyer where he states that tests
I came across this snippet of code on MSDN: entityBuilder.Metadata = @res://*/AdventureWorksModel.csdl| res://*/AdventureWorksModel.ssdl| res://*/AdventureWorksModel.msl;
I came across this very odd situation, and i thought i would throw it

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.