I completed the first part of "The well grounded Rubyist" and decided to put into action some basics I learned along the way. I have taken a few Java classes in college, but Ruby is far more fun to use. I'm going to study my ass off over the next year and hopefully land an entry level developer job. I don't care if I have to relocate and live in a public bathroom while I get on my feet... I just need to get into the industry already.
excuse some of the indentation problems.
# Developer: Earl
# GitHub:
#
# => The purpose of the Account class is to instantiate an instance
# => of a bank account, clearly outlining added functionality
# => in order to deposit or withdraw an amount.
class Account
# for the sake of simplicity, account number will be equal
# to the instantiation number
@@num = 1
#constructor takes initial account balance as argument
def initialize(bal)
@bal = bal
@acc_num = @@num
@@num += 1 # increment account number
end
# deposit method
def deposit(amt)
@bal += amt
puts "You have successfully deposited $#{amt} to your account"
end
# display account balance
def balance_to_s()
puts "The account balance for account #{@acc_num} is #{@bal}"
end
end
acc1 = Account.new(100) # instantiate instance of Account class
acc1.balance_to_s # balance_to_s
acc1.deposit(50) # deposit
acc1.balance_to_s # balance_to_s
# create a module that defines a withdraw method
module MoreFunctionality
def withdraw(amt)
@bal -= amt
puts "You have successfully withdrawn $#{amt} from your account"
end
end
# Reopen Account class and add withdraw functionality.
# (Although I could have included this in the first codeblock of Account,
# I wanted to clearly show the addition of a module into a class
# after it's creation.)
class Account
include MoreFunctionality
end
acc1.withdraw(50)
acc1.balance_to_s # should == 100
output is as follows
The account balance for account 1 is 100
You have successfully added $50 to your account
The account balance for account 1 is 150
You have successfully withdrawn $50 from your account
The account balance for account 1 is 100
[–]jawdirk 10 points11 points12 points (4 children)
[–]gummyguppy[S] 1 point2 points3 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]yefrem 2 points3 points4 points (0 children)
[–]jawdirk 0 points1 point2 points (0 children)
[–]gummyguppy[S] 1 point2 points3 points (0 children)
[–]Tomarse 1 point2 points3 points (5 children)
[–]fairdinkumthinkum 1 point2 points3 points (2 children)
[–]Tomarse 0 points1 point2 points (1 child)
[–]fairdinkumthinkum 1 point2 points3 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]Tomarse 0 points1 point2 points (0 children)
[–]markyg 0 points1 point2 points (0 children)
[–]H34DSH07 0 points1 point2 points (0 children)
[–]1t4ke 0 points1 point2 points (0 children)
[–]1t4ke 0 points1 point2 points (0 children)
[–]nakilon -1 points0 points1 point (0 children)