Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stuck on LearnStreet Ruby Training. Simple Ruby Code

Tags:

ruby

Attempting to learn Ruby using the new LearnStreet online tutorials.

Have attempted to get help via their Q&A system but it seems nobody answers them.

"Can you now implement the withdraw! method on account object, which takes one parameter amount and reduces the balance by the specified amount? After defining the method, go ahead and withdraw 100 dollars from the account and check the balance."

Is the question and I got the two hints of

" Hint 1 The code @balance = @balance - amount reduces the amount from @balance.

Hint 2 Then call the method withdraw! on the account object - account.withdraw!(100). "

My attempt was

def

account.widthdraw!

@balance = @balance - amount

end

account.withdraw!(100)

Any ideas what I'm missing?

like image 669
user1739696 Avatar asked Nov 04 '12 23:11

user1739696


2 Answers

"Can you now implement the withdraw! method on account object, which takes one parameter amount and reduces the balance by the specified amount? After defining the method, go ahead and withdraw 100 dollars from the account and check the balance."

One step at a time:

  • "Can you now implement the withdraw! method on account object

    class Account
      def withdraw!
      end
    end
    
  • which takes one parameter amount...

    class Account
      def withdraw!(amount)
      end
    end
    
  • and reduces the balance by the specified amount?

    class Account
      def withdraw!(amount)
        @balance = @balance - amount
      end
    end
    
  • After defining the method, go ahead and withdraw 100 dollars from the account and check the balance."

    account = Account.new
    account.withdraw!(100)
    
like image 129
danneu Avatar answered Oct 12 '22 00:10

danneu


I think you'd want something like this.

class Account

    def withdraw! amount
         @balance -= amount
    end

end
like image 44
alex Avatar answered Oct 12 '22 00:10

alex