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]]