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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:28:37+00:00 2026-06-18T16:28:37+00:00

class Foo def bar @instance_variable = [[‘first’]] # make a duplicate object with the

  • 0
class Foo
  def bar
    @instance_variable = [['first']]

    # make a duplicate object with the :dup method
    local_variable=@instance_variable.dup

    # They have different object_id
    p @instance_variable.object_id
    p local_variable.object_id


    local_variable.each{|n|n.push('second')}
    @instance_variable
  end
end

f=Foo.new
p f.bar

=> 2000
=> 2002
=> [["first", "second"]]

It seems that the local_variable still references to the @instance_variable, although it is a different object. This behaviour is both with the push and unshift in the each block. With a normal assignment like local_variable='second', the result is as expected => [['first']]

I don’t understand why local_variable.each{|n|n.push('second')} has an effect on the @instance_variable

Using Ruby-1.9.2p318

  • 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-18T16:28:38+00:00Added an answer on June 18, 2026 at 4:28 pm

    Both local_variable and @instance_variable have references to the same object, the inner array ['first']. And because it’s a mutable Array, you can effect changes to one array through the other.

    Object#dup in Ruby provides a shallow copy. In order to make a deep copy of an Array, you’d need to write some code (or find a library) that recursively walks the data structure, deep-cloning its pieces of mutable state.

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

Sidebar

Related Questions

I have the following structure: class foo(object): class bar(object): def __init__(self, parent): self._parent=parent #this
class Foo(object): __slots__ = ('a',) class Bar(Foo): @property def a(self): return super(Bar, self).a super(Bar,
I have following code: class Foo(object): def __init__(self): baz=self.bar(10) @staticmethod def bar(n): if n==0:
Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo()
i have a structure like, class Foo(object): def __init__(self, value=True): if value: Bar() else:
Can one write something like: class Test(object): def _decorator(self, foo): foo() @self._decorator def bar(self):
I am trying to get definition of method :foo from Class object. class Bar
class Foo(object): def tick(self): print(something) class Bar(object): def __init__(self): self.foo = Foo() def tick(self):
For example, suppose I have the code: class Foo(object): def bar(self, x): '''blah blah
class Foo def initialize(a) puts Hello #{a} end end module Bar def initialize(b) puts

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.