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 Objects and Classes Build a Bank Account Class Part 3: Keeping Our Balance

Adding a space causes an error!

HI! I noticed that in the add_transaction method definition, if I add a space between push and the parenthesis (see below)

    def add_transaction(description, amount)
        @transactions.push ( description: description, amount: amount )
    end

it causes this error:

bank_account.rb:11: syntax error, unexpected tLABEL @transactions.push ( description: description, amount: amount ) ^ bank_account.rb:36: syntax error, unexpected end-of-input, expecting keyword_end puts bank_account.balance

instead if I remove the space between push and the parenthesis:

    def add_transaction(description, amount)
        @transactions.push( description: description, amount: amount )
    end

It goes without errors! Could someone explain me why? :-)

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Francesco,

Besides convention and syntax of Ruby, I think it's because Ruby is a interpreted language and .push is a method call. But, when you have a space, the interpreter is unsure if you mean push as a method call with is parameters or maybe a key word with some invocations. So, it may not error out all the time, but in some situations (where there may be ambiguity), it could error out. So, it's just best to always not have a space.

When you don't have a space, there will be no doubts that what is in the parenthesis is attached to a method call and should be treated as such.

Sorry it's not more technical, this is just how I see the reasoning. Hope that helps.

:dizzy: