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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:01:19+00:00 2026-05-27T07:01:19+00:00

I am struggling with some strange variable behavior in a recursive instance method. Running

  • 0

I am struggling with some strange variable behavior in a recursive instance method. Running Ruby 1.8.7. I’m doing this as a learning exercise and have hit a bit of a wall. Any advice would be appreciated.

This code tries to figure out all of the different ways you can make a row of bricks with a length of max_length using bricks of lengths 1 ft, 2 ft, 3 ft, and 4ft.

The instance method add_block_to_row([0],10) will recursively add bricks of different lengths until putting together all possible combinations that satisfy the total length criteria. I am trying to store all of the different combinations of bricks in an instance variable, @rows, which is an array of arrays each representing a series of bricks in a row.

Everything works fine, EXCEPT, when I look at the rows variable of the instance it’s full of a bunch of [0] arrays. The strange thing is that when I change @rows.push(row) to @rows.push(row.to_s) everything works fine. What’s going on here? Why do the row arrays become empty when I try to put them in @rows? Below is the code and sample output for the two scenarios described here.

class Array; def sum; inject( nil ) { |sum,x| sum ? sum+x : x }; end; end

class Brick
  attr_accessor :rows

  def initialize(rows)
    @rows = rows
  end

  def add_brick_to_row(row,max_length)
    (1..4).each do |n|                                                                                                                                         
      # Add a brick if needed                                                                                                                                      
      if row.sum <= max_length
        row.push(n)
        if row.sum == max_length
          # If the row is complete add it to the rows matrix                                                                                                       
          # BELOW IS THE PROBLEMATIC LINE
          @rows.push(row)
          # EVERYTHING SEEMS TO WORK FINE WHEN I CHANGE row TO A STRING (SEE NEXT LINE)
          # @rows.push(row.to_s)
          print "pushed row: "
          print row.to_s+"\n"
        elsif row.sum < max_length
          # Recurse if more bricks are needed                                                                                                                       
          # print "adding brick\n"                                                                                                                                 
          self.add_brick_to_row(row, max_length)
        end
      end
      row.pop
    end

  end

end

Here’s the output running in IRB.
Normal case (where I have @rows.push(row.to_s)):

>> l.add_brick_to_row([0],3)
>> l.rows
=> [0, "0111", "012", "021", "03"]

Pathological case (where I have @rows.push(row)):

>> l.add_brick_to_row([0],3)
>> l.rows
=> [[0], [0], [0], [0]]
  • 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-27T07:01:20+00:00Added an answer on May 27, 2026 at 7:01 am

    The problem is that you are pushing multiple references to the same row array into rows

    Replace the push line with the following:

    @rows.push(Array.new(row))

    This will create a new array instance for each row and should fix your problem.

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

Sidebar

Related Questions

I have been struggling for some time with the strange behavior of navigation bar
I'm struggling getting some unit tests running and wondering if anyone might have anything
I have been struggling with this for quite some time having been accustomed to
I have been struggling with this for quite some time now. jQuery.ajax in the
Hi I'm struggling with some regex I've got a string like this: a:b||c:{d:e||f:g}||h:i basically
For some reason I'm really struggling with this. I'm new to wpf and I
I have been struggling to think of some decent uses for things like vectors
This is a bit of a strange one, but I've been struggling for a
I've been struggling with some weird behavior of fop 0.95 (don't know if I'm
Apologies about this simple question. Still struggling with some of the memory concepts here.

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.