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 Foundations Objects, Classes, and Variables Objects and Classes

Where do I get the file I am using? (RUBY)

I see Jason Seifer using this Console/Terminal with orange letters on his videos to print out what he does on the text editor. I am using the local terminal on my Mac but I am unable to print out anything once I run $ruby bank_account.rb on my terminal.

I've crated a .rb document to do it the same as in the video. The message that my terminal is giving me "ruby: No such file or directory -- bank_account.rb (LoadError)"

Am I using the proper tools or what is wrong?

6 Answers

Hi, Marcelo:

In your ruby file you spelled initialize wrong. As a result, when you use BankAccount.new, you won't get the output you wanted.

As far as executing your Ruby, try adding the flags -cw to enable syntax checking before a normal ruby call:

ruby -cw bank_account.rb 
=> syntax OK
ruby bank_account.rb

I GOT IT! THANK YOU AND GREAT TIP.

you forgot an i in initialize

In there Terminal you need to be in the directory that you save your bank_account.rb file to.

Sorry but I did not understand.

Marcelo Retana : You need to make sure you're in the same directory (aka folder) as the file you're trying to run. Usually your terminal will drop you in your home directory. On a Mac it's the directory with you Documents, Music, Downloads, etc folders.

For more information on getting around in the terminal you may want to check out the Console Foundations Course if you haven't done so already.

I have it already but now I am receiving this error on my terminal:

MacBook-Pro-de-MarceloRS:Desktop Marcelo$ ruby bank_account.rb

bank_account.rb:21:in initialize': wrong number of arguments (1 for 0) (ArgumentError) from bank_account.rb:21:innew' from bank_account.rb:21:in `<main>'

MacBook-Pro-de-MarceloRS:Desktop Marcelo$

and this is my code

class BankAccount

    def intialize(name)
        @transaction = []
        @balance = 0
    end

    def deposit
        print "how much would you like to deposit? "
        amount = gets.chomp
        @balance += amount.to_f
        puts "$#{amount} deposited."
    end

    def show_balance
        puts "your balance is #{@balance}"
    end

end

bank_account = BankAccount.new("Marcelo Retana")
bank_account.class # => BankAccount

bank_account.deposit
bank_account.show_balance

Your code still has the method initialize misspelled.

Right now, by convention (a crude simplification), Ruby is looking for a method called initialize to know what to do with a new call outside of the core ones defined in BasicObject, Object, or Kernel, what every object in Ruby inherits from.

By default, initialize as defined by likely the Kernel module accepts 0 arguments. Since Ruby saw the default initialize needing no arguments, but you passed one anyway, an exception happened.

Does this make sense, Marcelo?

I am also having issues. Every time that I run the bank_account.rb in my terminal, it's asks me for an amount to deposit. Once I enter an amount, I get the following message:

bank_account.rb:11:in `deposit': undefined method `+' for nil:NilClass (NoMethodError)
    from bank_account.rb:22:in `<class:BankAccount>'
    from bank_account.rb:1:in `<main>'

What am I doing wrong? Here is my code:

class BankAccount

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

  def deposit
    print "How much would you like to deposit? "
    amount = gets.chomp
    @balance += amount.to_f
    puts "$#{amount} deposited."
  end

  def show_balance
    puts "Your balance is #{@balance}"
  end

  bank_account = BankAccount.new("Mason Goetz")
  bank_account.class # => BankAccount

  bank_account.deposit
  bank_account.show_balance
end

I know that there is no end after the bank_account.show_balance on the last line in the video, but when I ran the code without it in terminal I got this message:

bank_account.rb:23: syntax error, unexpected end-of-input, expecting keyword_end
  bank_account.show_balance
                           ^
Andres Morales
Andres Morales
10,634 Points

Mason, you're missing an @ before balance in the fourth line, and the 'end' should go right after the following

 def show_balance
    puts "Your balance is #{@balance}"
  end

change it to

 def show_balance
    puts "Your balance is #{@balance}"
  end
end

Duh. Thanks Andres Morales