Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Ruby - System Stack Error

I seem to be getting a "stack level too deep (SystemStackError)" when I try to run this code. I am currently in the "Ruby Foundations: Standard Library" badge. Any thoughts?

require 'erb'

class BankAccount
    TEMPLATE = <<-TEMPLATE

Bank Account: <%= @name %>
---
<%= @transactions.each do |transaction| %>
Transaction: <%= transaction %>
<% end %>
---
    TEMPLATE

        def initialize(name)
        @name = name
        @transactions = []
    end

    def deposit(amount)
        @transactions.push(amount)
    end

    def withdraw(amount)
    @transactions.push(-amount)
    end

    def get_binding
        get_binding
    end

    def display
        ERB.new(TEMPLATE).result(get_binding)
    end
end


account = BankAccount.new("Jason Seifer")
account.deposit(100)
account.withdraw(2)
account.withdraw(30)
account.deposit(200)
puts account.display

2 Answers

Hi Scott,

I think you are calling get_binding from itself (infinite recursion)?

def get_binding
    get_binding
end

Tony

Ah that was it - thanks for the second set of eyes!

Adam