Day 30, Part 1: Continuing to Learn Ruby!

So, Friday and Saturday I have family coming to visit. I will try to study some, but I will probably have less time then usual. I have a lot to do today to prepare for the visit, but I am also committed to studying and transitioning to a web development career. Let’s get started at Treehouse:

I am in the “Build a Bank Account Class” stage, so let’s begin doing it. First video: Part 1: Create the Class

Here is the code:

And the result:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby bank_account.rb
#

And the challenges:

Challenge Task 1 of 3

Create a new, empty class called BankAccount.

Challenge Task 2 of 3

Create an initialize method for the BankAccount class that takes one argument, name, which sets @name as an instance variable on initialization.

Challenge Task 3 of 3

In the initialize method, create an empty array called @transactions, which is an instance variable.

Video 2: Part 2: Transactions, credits, and debits

The code:

and the results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby bank_account.rb
#“Beginning Balance”, :amount=>0}, {:description=>”Paycheck”, :amount=>100}, {:description=>”Groceries”, :amount=>-40}]>

Challenges:

Challenge Task 1 of 2

Create a method called add_transaction on the BankAccount class. The method should take two arguments: description and amount.

Challenge Task 2 of 2

Now that the add_transaction method exists, make the add_transaction method append a hash to the @transactions array. The hash should have the keys description and amount, as symbols, and values that match the arguments to the method.

Video 3: Part 3: Keeping Our Balance

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby bank_account.rb
Name: Nick, Balance: 60.00

Challenge Task 1 of 1

In the BankAccount class, implement a method called balance. The balance method should loop through transactions and calculate the current account balance.

Video 4: Part 4: Printing the register

The code:

The results:

demosthenes131@rails-tutorial:~/workspace/TreeHouse $ ruby bank_account.rb
Name: Nick, Balance: 49.49
Register:
Nick’s Bank Account
________________________________________
Description Amount
Beginning Balance 0.00
Paycheck 100.00
Groceries -40.00
Gas -10.51
________________________________________
Balance: 49.49
________________________________________

and the last challenge:

Challenge Task 1 of 1

Implement a to_s method on the BankAccount class that displays the name and balance in the following format (without brackets):
Name: [name], Balance: [balance]
It is not necessary to use the sprintf method to implement the balance printing.

Now, on to Learn Ruby the Hard Way,

So, first, the error above is he left off the “?” in the first include. Fixed.

The result:

demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex35.rb
You are in a dark room.
There is a door to your right, left or forward.
Which one do you take?
>right
Here you see the great evil Cthulhu.
He, it, whatever stares at you and you go insane.
Do you flee for your life or eat your head?
>flee
You are in a dark room.
There is a door to your right, left or forward.
Which one do you take?
>left
There is a bear here.
The bear has a bunch of honey.
The fat bear is in front of another door.
How are you going to move the bear?
>take honey
The bear looks at you then slaps your face off.
Good job!
demosthenes131@rails-tutorial:~/workspace/LRTHW $ ruby ex35.rb
You are in a dark room.
There is a door to your right, left or forward.
Which one do you take?
>left
There is a bear here.
The bear has a bunch of honey.
The fat bear is in front of another door.
How are you going to move the bear?
>taunt bear
The bear has moved from the door. You can go through it now.
>open door
This room is full of gold. How much do you take?
>49
Man, learn to type a number.
Good job!

Hmm.

Study Drills

1. Draw a map of the game and how you flow through it.

2. Fix all of your mistakes, including spelling mistakes.

Done.

3. Write comments for the functions you do not understand.

I think I get the functions except for in the gold room.

4. Add more to the game. What can you do to both simplify and expand it?

I will come back to this.

5. The gold_room has a weird way of getting you to type a number. What are all the bugs in this way of doing it? Can you make it better than what I’ve written? Look at how =~ works for clues.

The issue here is that I did type a number above and it did not see it. I am running out of time and need to get back to getting ready for company. I need to think about this some more…

2 thoughts on “Day 30, Part 1: Continuing to Learn Ruby!

  1. I’m pretty much at the same place as you on both Treehouse and Hard Way, and I’m stuck on this exact part myself!
    Did you manage to solve it?
    I can’t figure out how to check that the user has entered a number, and not letters.
    He says “Look at how =~ works to figure it out” but I can’t find no information on “=~”.

    John

Comments are closed.