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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:48:56+00:00 2026-06-04T13:48:56+00:00

I’ve wrote two ruby files for test test.rb: #!/usr/bin/ruby def foo(bar) bar[‘key’] = ‘value’

  • 0

I’ve wrote two ruby files for test

test.rb:

#!/usr/bin/ruby

def foo(bar)
    bar['key'] = 'value'
end

def my_print(a)
    a.each{|k,v|
        puts "#{k} => #{v}"
    }
end

test_drive.rb:

#!/usr/bin/ruby

require 'test.rb'

hash_test = Hash.new

foo(hash_test)

my_print(hash_test)

It works as what I expect, the output is

key => value

But when I chanege the test.rb to

#!/usr/bin/ruby

def foo(bar)
    pre_defined = {'key' => 'value'}
    bar = pre_defined
end

def my_print(a)
    a.each{|k,v|
        puts "#{k} => #{v}"
    }
end

Here I used a pre-defined hash, but now it outputs nothing. The “hash_test” is now an empty hash.
Please illustrate me why indeed does this happen?

  • 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-04T13:48:58+00:00Added an answer on June 4, 2026 at 1:48 pm

    Here is the simple answer:

    In ruby you pass variables with Objects by reference. When you assign an Object to a variable, the variable does not really contain the Object itself within it. Instead, it contains only a reference to that Object.

    It may help for you to start seeing the differences between references and objects in order to understand how sending variables as parameters works: the Object itself does not reside in the variable, the variable is pointing to a reference of the Object in memory, because of this, if one variable points to another object and modifies it, that doesn’t mean it modified the Object that it was previously referring to.

    The important thing for you to understand is that the bar parameter in the foo method is really only a REFERENCE to the Object in memory, not the Object itself. So if bar once pointed to the Object, but it’s now referencing another Object, it will modify what it is referencing, not the previous object it pointed to. Here’s the code of the last test.rb commented slightly for you to understand it better:

    def foo(bar) # here "bar" points to the same object as "hash_test"
        pre_defined = {'key' => 'value'}  # here a new object is created and referenced
        bar = pre_defined # now "bar" points to a new object and forgets about "hash_test"
    end
    
    def my_print(a) # here "a" holds a reference to "hash_test" which is empty
        a.each{|k,v|  # "a" is empty, so it has nothing to put
            puts "#{k} => #{v}" 
        }
    end
    

    I hope this helps. In case you need something a little more detailed:

    The reason your last version of test.rb prints an empty hash in test_drive.rb is because the Hash Object that the reference “hash_test” points to is not really being modified at all. Instead the foo method, while initially receiving as a parameter called “bar” a reference of the Hash Object that “hash_test” points to, quickly replaces that reference in “bar” with a brand new reference to a brand new Hash Object that the “pre_defined” variable points to. Now “bar” doesn’t point to the same Object as “hash_test” does, and so the Object that “hash_test” points to is never being modified. Thus, the Hash Object that “hash_test” contains is never really populated with anything, and is empty when my_print tries to print it.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a bunch of posts stored in text files formatted in yaml/textile (from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.