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.
Related
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.
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.
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.
I have spent the last two weeks building a Sinatra app, and I have hit my first major roadblock. I have spent the last 3 days trying to find a solution, but need to ask for help.
I am building a small game using Sinatra, and I am having trouble understanding a concept.
The game tracks rounds and scores.
Here is some of my code:
#Player Class
class Player
attr_accessor :name, :guess, :round_score, :total_score
def initialize
#guess = 0
#round_score = 0
#total_score = 0
end
end
#Tracks Round
class RoundCount
attr_accessor :round
def initialize
#round = 0
end
def count
#round += 1
end
end
helpers do
#New game function. Makes set number of player classes in ##player array.
def new_game(players)
##i = RoundCount.new
##player = []
players.times do |x|
##player[x] = Player.new
##player[x].name = "Player#{x}"
end
end
#Calculates score
def player_score()
params.each do |x, y|
##player.each do |z|
if z.name == x
z.guess = y.to_i
z.round_score = (y.to_i - ##movie[##i.round].ratings['critics_score'].to_i).abs
z.total_score += (y.to_i - ##movie[##i.round].ratings['critics_score'].to_i).abs
end
end
end
end
get '/' do
slim :home
end
get '/players' do
slim :players
end
post '/numplayers' do
new_game(params[:numplayers].to_i)
slim :names
end
post '/names' do
slim :titles
end
Currently the program gives me the error "warning: class variable access from topelevel" and is affected by other users who play at the same time on seperate computers.
I need help understanding how to track players scores and the round of the game in each view, in a way that isn't affected by other players running a different game.
Any suggestions on how to avoid using class variables for round and score, while retaining a way to access them in each route and view?
Use Sessions or Cookies
Generally, keeping server-side state through class variables will make your life harder. There's also usually more than one way to do anything, but for your use case I'd recommend that you store your state in a session variable or a cookie.
You could then store your state in any serializable object, such as a Hash, Array, Struct, or OpenSruct. This will allow for concurrent users, and do away with the need for the class variables you're currently using.
So I'm trying to write some code to make an RSPEC test pass for Ruby. But I'm having some trouble even getting the first test to pass. I think if I can have a little help with this, I'll be able to get the rest. But I can post the rest of the tests if that makes it easier to offer advice.
So it's constructing a fahrenheit/celsius converter, but using objects and classes instead of just defining a couple of methods to do the conversions.
The first part looks like this
require "temperature"
describe Temperature do
describe "can be constructed with an options hash" do
describe "in degrees fahrenheit" do
it "at 50 degrees" do
Temperature.new(:f => 50).in_fahrenheit.should == 50
end
One of the hints in the instructions says that the Temperature object constructer should accept an options hash with either a :celsius or :fahrenheit entry.
Any help or hints would be greatly appreciated. I've been stuck on this test for the last few weeks. Thanks in advance.
I think your Temperature class needs some work. Why not have a 'scale' attribute that can set the base value of your Temperature object?
Here's a modified version of what you've posted:
class Temperature
attr_accessor :value, :scale
def initialize(value, options={})
#value = value
if options.has_key?(:scale)
#scale = options[:scale]
else
#scale = :c
end
end
def in_fahrenheit()
if #scale == :c
( #value * 9.0/5.0 ) + 32.0
else
#value
end
end
end
There's no need to create a new Temperature object when you call #in_fahrenheit. You can make your current object convert a number (stored in an attribute) into fahrenheit. You can add the temperature information when you create your object:
t1=Temperature.new(68, :scale =>:f)
t2=Temperature.new(0)
2.0.0dev :199 > t1.in_fahrenheit => 68
2.0.0dev :200 > t2.in_fahrenheit => 32.0