Basic Ruby questions - ruby

My understanding is that checking and savings are saved under account_a and account_b. Can someone explain what's happening in the last two lines?
class Account
attr_accessor :balance
def initialize(balance)
#balance = balance
end
end
class Transaction
def initialize(account_a,account_b)
#account_a = account_a
#account_b = account_b
end
private
def debit(account, amount)
account.balance -= amount
end
def credit(account,amount)
account.balance += amount
end
public
def transfer(amount)
debit(#account_a, amount)
credit(#account_b, amount)
end
end
savings = Account.new (100)
checking = Account.new (200)
trans = Transaction.new(checking, savings)
trans.transfer(50)
How do I print the value of trans?

In the second to last line, you are initializing a new Transaction from your checking account to your savings account. Then in the last line, you are transferring 50.
You cannot print the value of trans because there is no value associated with it. You can print out the balance of your initialized accounts though, by calling savings.balance and checking.balance

When you create trans using Transaction.new then references to checking and savings are stored inside the transaction object as #account_a and #account_b
The transfer method calls debit with #account_a and credit with #account_b so decreasing the balance of a by the amount and increasing the balance of b by the same amount, in effect transferring from checking to savings.

Related

Ruby using Elapsed Time to increase Obj stats

I'm still new to Ruby and I'm having some issues working with time. Essentially the goal of this project is to make a virtual pet. I have class Pet set up at that when initialized randomly generates stats for the pets hunger, energy, fun, affection, and then will determine the pets mood.
In my class Game, the user is displayed the pets current stats and then asked for input to interact with the pet. The user input takes the string (e.g. 'Feed') and then will decrease #pet.hunger by 1.
What I want to do is create some way of storing the time elapsed since the user last interacted with a stat. If the stat has not been interacted with within 2 minutes it will be increased or decreased accordingly and the time will restart. Here is the module I attempted to use
module FeedTime
def feed_time
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
#user_input == 'Feed'
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
time_elapsed = end_time - start_time
#pet.hunger += 1 unless time_elapsed < 120
end
end
This is how it was used within the Game class
require_relative 'time'
class StartGame
include Reaction
include FeedTime
attr_reader :dragon
def initialize
#dragon = Dragon.new
end
def game
feed_time
loop do
welcome
user_input
break if #user_input == 'Quit'
end
end
I've tried a few variations of this code with no luck. All of my code runs without error but Hunger will never increase.

Ruby object oriented inheritance

I am writing a banking program. Already defined Bank class.
class Checking
def initialize(initial_deposit)
#number = Bank.size + 1
#principal = initial_deposit.to_f
##intest_rate = 0.003
end
def balance
principal = principal * (1 + interest_rate / 365 ) ** 365
end
end
class Savings
def initialize(initial_deposit)
#number = Bank.size + 1
#principal = initial_deposit.to_f
##interest_rate = 0.025
end
def balance
principal = principal * (1 + interest_rate / 4) ** 4
end
end
But keep getting error message.I'm pretty sure the issue is within Saving and Checking; when I press s or c, errors arise. Maybe someone could help find my error. Any help or advice would be appreciated.
Can you post the Bank class code here. I think the issue could be in the Bank class, not sure why are you pressing 's' and 'c'. The above code doesn't take any input.
And I assume that the Bank class asks the user to select the account type he need to create and in response to the key he presses, the Bank class might call the Saving and Checking class and create the account.

Turning a single file into MVC without Rails

I need to take the single file code below and separate it into a Model, View, Controller (MVC) ruby program that can run by the ruby command in the command line without using Rails (for instructions on how to run this program from irb, check out the README.md on my RubyBank Github Repo).
require_relative 'view'
class BankAccount
attr_accessor :name, :balance
def initialize(name, balance=0)
#name = name
#balance = balance
end
def show_balance(pin_access)
if pin_access == pin || pin_access == bank_manager
puts "\nYour current balance is: $#{#balance}"
else
puts pin_error_message
end
end
def withdraw(pin_access, amount)
if pin_access == pin
#balance -= amount
puts "'\nYou just withdrew $#{amount} from your account. \n\nYour remaining balance is: $#{#balance}\n\n"
else
puts pin_error_message
end
if #balance < 0
#balance += amount
return overdraft_protection
end
end
def deposit(pin_access, amount)
if pin_access == pin
#balance += amount
puts "\nYou just deposited $#{amount} into your account. \n\nYour remaining balance is: $#{#balance}"
else
puts pin_error_message
end
end
private
def pin
#pin = 1234
end
def bank_manager
#bank_manager = 4321
end
def pin_error_message
puts "Invalid PIN number. Try again."
end
def overdraft_protection
puts "\nYou have overdrafted your account. We cannot complete your withdrawl. Please deposit money before trying again. \n\nYour corrected balance is $#{#balance}"
end
end
I am looking for a good place to start or a general approach towards taking on such a task.
A simple approach would be to create three classes:
BankAccount minus text output is your Model.
All the text I/O goes into your View. Prompt the user for an action or registration. Get the model (for displaying data) from your controller or use the model directly.
Your Controller is responsible for a) reacting to user input, b) modifying the model and c) for holding state not directly related to the BankAccount (this point is discussable) like being logged in or what actions are possible from your current state. Your Controller receives all actions with user supplied data from your view.
Clean separation between View and Controller may be a bit hard in a console application. Also, there are about a million possible ways to implement this in a MVC style. Most important point: no UI-Code (puts/gets) in your model.

Learnstreet Ruby Lesson 11.5 Transfer state between objects

I've been stuck on this Learnstreet lesson for a day now. The exercise prompts:
Can you now implement a method called transfer! which takes two parameters, amount and other_account. The method should withdraw the specified amount from the current object and deposit it into other_account object.
The code in the editor goes as follows:
class BankAccount
attr_accessor :name, :balance, :address
def initialize(name, balance, address)
#name = name
#balance = balance
#address = address
end
def withdraw!(amount)
if #balance - amount > 0
#balance = #balance - amount
end
#balance
end
def deposit!(amount)
#balance += amount
end
# your code here
end
alices_account = BankAccount.new("Alice Cooper", 2500, "456 University Avenue")
bobs_account = BankAccount.new("Bob Ventura", 2100, "3500 Fox Street")
I know that you need to set up a method with def transfer!(amount, other_account). However I do not know what to put in the bottom after alices_account and bobs_account.
You'd call transfer! on one of the objects, passing in the other, e.g.,
bobs_account.transfer!(500, alices_account)
You're just calling a method on an instance, like "foo".size or [1, 2, 3].each etc. The only difference is that you've created the method you're calling.
I know that you need to set up a method with def transfer!(amount, other_account).
So basically you have to create BankAccount#transfer! that withdraw some money from the object that calls it and deposit the sum into the "other" BankAccount object.
The solution is pretty trivial since you have the BankAccount#withdraw! and BankAccount#deposit! already set up:
def transfer!(amount, other_account)
withdraw! amount
other_account.deposit! amount
end
However I do not know what to put in the bottom after alices_account and bobs_account.
The exercise doesn't require you to do anything with the latter. If you were supposed to do something you would need to know the amount of "money" to transfer from alices_account to bobs_account an viceversa and then go with:
# assuming x to be the amount to transfer
alices_account.transfer! x, bobs_account
or:
# assuming x to be the amount to transfer
bobs_account.transfer! x, alices_account
Ok now. I've spent an hour to complete all the 10 course before that one and this is what I discovered. At some point you get to write the last two lines of your code.
Then a weird thing happens. The code generated by the exercise contains a . To near the end which is obviously a syntax error. By removing that line and adding the method I provided above you get to pass the test.

Is it possible to verify the number of methods assigned to a Proc in Ruby?

I'm currently working on a DSL in relation to accounting. What I'd like to be able to do is:
accountant do
credit #account_1, -#amount
debit #account_2, #amount
end
Currently, this executes the following method:
class Accountant
def accountant &block
AccountantHelper.class_eval(&block)
end
end
...Which in turn executes the block on the AccountantHelper class, calling the "credit" and "debit" methods respectively:
class AccountantHelper
def self.credit account, amount
account.credit amount
end
def self.debit account, amount
account.debit amount
end
end
(Please hold back any fire about using class_eval() -- this is only a prototype after all!)
The goal is for the block to act as a transaction, ensuring that if the entire block can't be executed successfully, then none of it should. However in addition to this, it should also verify the integrity of the data passed into the block. In this case I need to verify that there is both a "credit" and a "debit" method within the block (in double-entry accounting, for every credit there must also be at least one debit, and vice versa). Currently I could call:
accountant do
credit #account_1, #amount
end
...And the code will execute without any errors. This would be a bad thing as there is no corresponding "debit" to keep the accounts in balance.
Is it possible to verify what gets passed into the block? Or am I heading down the wrong path here?
I guess you can make your credit and debit actions "lazy", so that they are executed by the wrapper method, after the validation. Here's a proof of concept, similar to yours, but without metaprogramming part, skipped for clarity:
def transaction
yield
if #actions.map(&:last).inject(&:+) == 0
#actions.each do |account, amount|
#accounts[account] += amount
end
#actions = []
puts 'transaction executed ok'
else
puts 'balance not zero, rolling back transaction'
# rollback (effectively, do nothing)
end
end
def credit account, amount
#actions << [account, amount]
end
def debit account, amount
#actions<< [account, -amount]
end
#actions = []
#accounts = {a: 0, b: 0, c: 0} # start with three blank accounts
transaction do
credit :a, 5
debit :b, 2
debit :c, 3
end
#=>transaction executed ok
p #accounts
#=>{:a=>5, :b=>-2, :c=>-3}
transaction do
credit :a, 5
debit :b, 4
end
#=> balance not zero, rolling back transaction
p #accounts
#=> {:a=>5, :b=>-2, :c=>-3}

Resources