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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:47:56+00:00 2026-05-30T01:47:56+00:00

I was going through Ruby Koans tutorial series , when I came upon this

  • 0

I was going through Ruby Koans tutorial series, when I came upon this in about_hashes.rb:

def test_default_value_is_the_same_object
  hash = Hash.new([])

  hash[:one] << "uno"
  hash[:two] << "dos"

  assert_equal ["uno", "dos"], hash[:one]
  assert_equal ["uno", "dos"], hash[:two]
  assert_equal ["uno", "dos"], hash[:three]

  assert_equal true, hash[:one].object_id == hash[:two].object_id
end

The values in assert_equals, is actually what the tutorial expected. But I couldn’t understand how there is a difference between using << operator and = operator?

My expectation was that:

  • hash[:one] would be ["uno"]
  • hash[:two] would be ["dos"]
  • hash[:three] would be []

Can someone please explain why my expectation was 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-30T01:47:57+00:00Added an answer on May 30, 2026 at 1:47 am

    When you’re doing hash = Hash.new([]) you are creating a Hash whose default value is the exact same Array instance for all keys. So whenever you are accessing a key that doesn’t exist, you get back the very same Array.

    h = Hash.new([])
    h[:foo].object_id # => 12215540
    h[:bar].object_id # => 12215540
    

    If you want one array per key, you have to use the block syntax of Hash.new:

    h = Hash.new { |h, k| h[k] = [] }
    h[:foo].object_id # => 7791280
    h[:bar].object_id # => 7790760
    

    Edit: Also see what Gazler has to say with regard to the #<< method and on what object you are actually calling it.

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

Sidebar

Related Questions

I was going through an example from the Programming in Ruby book: def fib_up_to(max)
I'm going through the tutorial Bastards Book of Ruby and I'm having trouble understanding
I'm going through this tutorial: http://tutorials.jumpstartlab.com/projects/jsattend.html In iteration 7, step 3 we get to
I'm trying to learn ruby on rails. I've been going through a tutorial, but
I am going through the definitive ROR tutorial as I am completely new to
I'm going through the ruby koans, I'm on 151 and I just hit a
I'm going through about_hashes.rb from RubyKoans . 1 exercise got me puzzled: def test_default_value
I'm going through Michael Hartl's Ruby on Rails 3.2 Tutorial and I'm confused about
I'm going through the second edition of Ruby on Rails Tutorial (http://ruby.railstutorial.org/). I'm following
I was going through the Programming Ruby book and I'm having problems understanding the

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.