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

  • Home
  • SEARCH
  • 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 6711115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:06:05+00:00 2026-05-26T08:06:05+00:00

I’ve been practicing coding some algorithms (job interview time), and was coding the Huffman

  • 0

I’ve been practicing coding some algorithms (job interview time), and was coding the Huffman compression algorithm and ran into what seems like strange behaviour in the Ruby array class.

Once I have a built my heap I do a preorder traversal of my tree and store the symbols and binary values in a hash table. Here’s the method.

def encode(huffman_tree, encoded_chars, binary)
    if huffman_tree.is_leaf?
        encoded_chars[huffman_tree.symbol] = binary
    else
        encode(huffman_tree.left, encoded_chars, binary + '0')
        encode(huffman_tree.right, encoded_chars, binary + '1')    
    end
end

This works fine, but I had previously been passing in an empty array as the binary param and then appending to that array

def encode(huffman_tree, encoded_chars, binary)
    if huffman_tree.is_leaf?
        encoded_chars[huffman_tree.symbol] = binary
    else
        encode(huffman_tree.left, encoded_chars, binary << 0)
        encode(huffman_tree.right, encoded_chars, binary << 1)    
    end
end

I’ll grant you that it makes more sense to just use a string, but I thought I should be able to do the same thing with an array. However, when I ran the algorithm with binary as an array every recursive call to encode led to the binary array growing in size (I thought it should go out of scope every time the method returns) Here’s sample output run with binary as a string and then as an array. Can someone explain why this happens?

string
00000,
00001,
0001,
001,
01000,
01001,
0101000,
01010010

array
00000,
000001,
0000011,
00000111,
000001111000,
0000011110001,
00000111100011000,
0000011110001100010
  • 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-26T08:06:05+00:00Added an answer on May 26, 2026 at 8:06 am

    You’re misunderstanding how the operators work in this case:

    string = 'test'
    # => 'test'
    string + 'string'
    # => 'teststring'
    string
    # => 'test'
    string << 'string'
    # => 'teststring'
    string
    # => 'teststring'
    

    The String + method returns a new string representing the combined values. The Array << operator appends something to the array, it does not create a new copy.

    What you probably want is:

    binary + [ 0 ]
    

    This will create a new array, but it isn’t especially efficient.

    It is important to remember that variables in Ruby represent references to Objects, and that any value passed in as an argument to something is a shared reference. Other languages will implicitly duplicate all arguments to avoid this problem unless you use a pointer or some other kind of explicit reference.

    In other words, the array passed in to the method is identical to the array received by the method, so any modifications to it persist outside the scope of the method.

    • 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 ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6

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.