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 trialMarcelo Retana
2,534 PointsWhere 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
Kevin Lozandier
Courses Plus Student 53,747 PointsHi, 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
Shawn Anderton
24,124 Pointsyou forgot an i in initialize
Shawn Anderton
24,124 PointsIn there Terminal you need to be in the directory that you save your bank_account.rb file to.
Marcelo Retana
2,534 PointsSorry but I did not understand.
Geoff Parsons
11,679 PointsMarcelo 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.
Marcelo Retana
2,534 PointsI 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:in
new'
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
Kevin Lozandier
Courses Plus Student 53,747 PointsYour 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?
Mason Goetz
5,048 PointsI 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
10,634 PointsMason, 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
Mason Goetz
5,048 PointsDuh. Thanks Andres Morales
Marcelo Retana
2,534 PointsMarcelo Retana
2,534 PointsI GOT IT! THANK YOU AND GREAT TIP.