The below is my test in RSPEC. It is failing and I can't figure out why! I am trying to get it to fill the van with bikes upto it's capacity but leave all the other bikes at the station. However it is deleting all the bikes except one in each at the moment.
let(:van) { Van.new }
let(:bike) { double :bike, broken?: false }
let(:broken_bike) { double :broken_bike, broken?: true }
let(:station) { double :station }
it 'can be filled with broken bikes' do
station = double :station, { release_x_broken_bikes: broken_bike }
expect(station).to receive(release_x_broken_bikes: broken_bike)
van.fill_with_broken_bikes_from station
expect(van.bike_count).to eq > 2
end
Relevant code in van.rb:
def initialize bikes = [], capacity = 10
#bikes = bikes
#capacity = capacity
end
def bike_count
#bikes.count
end
def fill_with_broken_bikes_from station
#bikes = station.release_x_broken_bikes(slots_available)
end
def slots_available
#capacity - bike_count
end
Code in station.rb:
def broken_bikes
#bikes.select { |bike| bike.broken? }
end
def release_broken_bike
#bikes.delete(broken_bikes.first)
end
def release_x_broken_bikes(x)
broken_bikes[0,x].map{ release_broken_bike}
end
In your spec:
expect(station).to receive(release_x_broken_bikes: broken_bike)
You're telling the station to expect to receive a release_x_broken_bikes with an argument of broken_bike? Looking at the method, it wants a number. broken_bike looks like a double to me.
Your error tells you exactly this:
1) Van can be filled with broken bikes Failure/Error: van.fill_with_broken_bikes_from station Double :station received unexpected message :release_x_broken_bikes with (10) # ./lib/van.rb:29:in fill_with_broken_bikes_from' # ./spec/van_spec.rb:51:in block (2 levels) in ' Finished in 0.00995 seconds 12 examples, 1 failure
It received a call to release_x_broken_bikes with 10, which seems to be what you should actually want. Tell it to expect that, and you can get to your next problem (if there is one).
Related
I have this sample code, which is essentially just a few basic classes for working with mdii files
class Array
def to_midi(file, note_length='eighth')
midi_max = 108.0
midi_min = 21.0
low, high = min, max
song = MIDI::Sequence.new
# Create a new track to hold the melody, running at 120 beats per minute.
song.tracks << (melody = MIDI::Track.new(song))
melody.events << MIDI::Tempo.new(MIDI::Tempo.bpm_to_mpq(120))
# Tell channel zero to use the "piano" sound.
melody.events << MIDI::ProgramChange.new(0, 0)
# Create a series of note events that play on channel zero.
each do |number|
midi_note = (midi_min + ((number-midi_min) * (midi_max-low)/high)).to_i
melody.events << MIDI::NoteOnEvent.new(0, midi_note, 127, 0)
melody.events << MIDI::NoteOffEvent.new(0, midi_note, 127,
song.note_to_delta(note_length))
end
open(file, 'w') { |f| song.write(f) }
end
end
class TimedTrack < MIDI::Track
MIDDLE_C = 60
##channel_counter=0
def initialize(number, song)
super(number)
#sequence = song
#time = 0
#channel = ##channel_counter
##channel_counter += 1
end
# Tell this track's channel to use the given instrument, and
# also set the track's instrument display name.
def instrument=(instrument)
#events << MIDI::ProgramChange.new(#channel, instrument)
super(MIDI::GM_PATCH_NAMES[instrument])
end
# Add one or more notes to sound simultaneously. Increments the per-track
# timer so that subsequent notes will sound after this one finishes.
def add_notes(offsets, velocity=127, duration='quarter')
offsets = [offsets] unless offsets.respond_to? :each
offsets.each do |offset|
event(MIDI::NoteOnEvent.new(#channel, MIDDLE_C + offset, velocity))
end
#time += #sequence.note_to_delta(duration)
offsets.each do |offset|
event(MIDI::NoteOffEvent.new(#channel, MIDDLE_C + offset, velocity))
end
recalc_delta_from_times
end
# Uses add_notes to sound a chord (a major triad in root position), using the
# given note as the low note. Like add_notes, increments the per-track timer.
def add_major_triad(low_note, velocity=127, duration='quarter')
add_notes([0, 4, 7].collect { |x| x + low_note }, velocity, duration)
end
private
def event(event)
#events << event
event.time_from_start = #time
end
end
most of it makes perfect sense to me except for the lines that use the << operator, from all of my research the only reason to use a << is when your defining a class that will be a singleton. So in what way specifically is the << being used in this code?
From https://github.com/jimm/midilib:
MIDI::Track is a track that contains an array of events.
So with << you're adding events to your track. It is the same as
melody.events.push(MIDI::NoteOnEvent.new(0, midi_note, 127, 0))
<< could also be used for bit shifting operations
http://calleerlandsson.com/2014/02/06/rubys-bitwise-operators/
<< operator may be used either for bitwise operations (quite unlikely here) or may be overloaded inside the class to match some behavior. In this case there is (probably) an Array object and thus the event is pushed into the #events array via this operator. More info about this use case can be found here.
Take notice, that in future you can bump into other situations where this operator is used and not everytime it will mean same thing - it depends on library creator. The other use case that comes into mind right now can be ActiveRecord Relationships, as has_many where also you can use this operator to immediately push an object to relationship and save. More info about this one can be found in api docs here. Quick sample:
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
end
And then somewhere in code you can use:
#post = Post.find(10)
#user = User.find(1)
#user.posts << #post
# automatically save the #post object with updated foreign key
I'm new to programming in Ruby.
How do I make the output show Revenue and Profit or Loss?
How can I refactor the following code to look neater? I know it's wrong but I have no idea how to take my if profit out of the initialize method.
class Theater
attr_accessor :ticket_price, :number_of_attendees, :revenue, :cost
def initialize
puts "What is your selling price of the ticket?"
#ticket_price = gets.chomp.to_i
puts "How many audience are there?"
#number_of_attendees = gets.chomp.to_i
#revenue = (#number_of_attendees * #ticket_price)
#cost = (#number_of_attendees * 3) + 180
#profit = (#revenue - #cost)
if #profit > 0
puts "Profit made: $#{#profit}"
else
puts "Loss incurred: $#{#profit.abs}"
end
end
end
theater = Theater.new
# theater.profit
# puts "Revenue for the theater is RM#{theater.revenue}."
# I hope to put my Profit/Loss here
#
# puts theater.revenue
Thanks guys.
Do not initialize the object with input from the user, make your object accept the needed values. Make a method to read the needed input and return you new Theater. Last of all put the if in separate method like #report_profit.
Remember constructors are for setting up the initial state of the object, making sure it is in a valid state. The constructor should not have side effects(in your case system input/output). This is something to be aware for all programming languages, not just ruby.
Try this:
class Theatre
COST = { running: 3, fixed: 180 }
attr_accessor :number_of_audience, :ticket_price
def revenue
#number_of_audience * #ticket_price
end
def total_cost
COST[:fixed] + (#number_of_audience * COST[:running])
end
def net
revenue - total_cost
end
def profit?
net > 0
end
end
class TheatreCLI
def initialize
#theatre = Theatre.new
end
def seek_number_of_attendes
print 'Number of audience: '
#theatre.number_of_audience = gets.chomp.to_i
end
def seek_ticket_price
print 'Ticket price: '
#theatre.ticket_price = gets.chomp.to_i
end
def print_revenue
puts "Revenue for the theatre is RM #{#theatre.revenue}."
end
def print_profit
message_prefix = #theatre.profit? ? 'Profit made' : 'Loss incurred'
puts "#{message_prefix} #{#theatre.net.abs}."
end
def self.run
TheatreCLI.new.instance_eval do
seek_ticket_price
seek_number_of_attendes
print_revenue
print_profit
end
end
end
TheatreCLI.run
Notes:
Never use your constructor (initialize method) for anything other than initial setup.
Try to keep all methods under 5 lines.
Always try to keep each class handle a single responsibility; for instance, printing and formatting output is not something the Theatre class needs to care.
Try extracting all hard coded values; eg see the COST hash.
Use apt variables consistent to the domain. Eg: net instead of profit makes the intent clear.
I am creating a simple CC class that can create and update a credit card. To do this, I have created cc_bal{} as an instance object so it can update respect credit cards. The hash is to save and update a person and the amount on their cc. I end up getting an output of just the original amount that was created and not the updated amount
Heres the code:
class CC
def initialize(cc_name, cc_bal = {}, open_cc = false)
#cc_name = cc_name
#cc_bal = cc_bal
#open_cc = open_cc
end
def create(initAmount, person)
if initAmount > 0
#open_cc = true
#cc_bal[:person]=initAmount
puts "congrats #{person} on opening your new #{#cc_name} CC! with $#{#cc_bal[:person]}"
else
puts "sorry not enough funds"
end
end
def update(amount, person)
if #open_cc == true
#cc_bal[:person] + amount
else
puts "sorry no account created, #{person}"
end
puts "#{person} has CC balance of #{#cc_bal[:person]}"
end
end
#chase = Bank.new("JP Morgan Chase")
#wells_fargo = Bank.new("Wells Fargo")
me = Person.new("Shehzan", 500)
friend1 = Person.new("John", 1000)
#chase.open_account(me)
#chase.open_account(friend1)
#wells_fargo.open_account(me)
#wells_fargo.open_account(friend1)
#chase.deposit(me, 200)
#chase.deposit(friend1, 300)
#chase.withdraw(me, 50)
#chase.transfer(me, wells_fargo, 100)
#chase.deposit(me, 5000)
#chase.withdraw(me, 5000)
#puts chase.total_cash_in_bank
#puts wells_fargo.total_cash_in_bank
credit_card = CC.new("Visa")
credit_card.create(10,me)
credit_card.update(50,me)
credit_card.create(20,friend1)
credit_card.update(40,friend1)
Please disregard the function calls that are commented out.
Any idea why the CC's are not updatiing?
if #open_cc == true
#cc_bal[:person] + amount
else
You increase the amount, but you don't set the new value anywhere. It should be
if #open_cc == true
#cc_bal[:person] += amount
else
Note. The code needs some serious refactoring and cleanup.
Forgive me if my question isn't completely clear. I have been awake for way too long and I'm feeling a little brain dead.
I'm doing a Ruby exercise and I can't figure out why my rspec test isn't passing for something I thought would work.
require 'date'
class Product
attr_accessor :photo_src, :promotion, :initial_date
attr_reader :default_photo, :default_price, :current_price
def initialize(name, photo, price)
#name = name
#default_photo = photo
#photo_src = photo
#default_price = price
#current_price = price
#initial_date = Date.today.yday
#promotion = false
end
def price_change(sale_price)
calculator = RedPencilCalculator.new(self)
if promotion
if sale_price > #current_price
calculator.end_promotion!
elsif sale_price < (#default_price - (#default_price * 0.3))
calculator.end_promotion!
end
else
calculator.start_promotion!
end
#current_price = sale_price
end
end
class RedPencilCalculator
attr_accessor :promotion_start, :product
def initialize(product)
#product = product
end
def start_promotion!
if start_promotion?
product.promotion = true
product.photo_src = "redX.png"
#promotion_start = Date.today.yday
end
end
#would need to run daily
def end_promotion?
promotion_duration
if #duration == 30 || #duration == 335
end_promotion!
end
end
def end_promotion!
product.promotion = false
product.photo_src = product.default_photo
product.initial_date = Date.today.yday
end
private
def calculate_range
#min_discount = product.default_price - (product.default_price * 0.05)
#max_discount = product.default_price - (product.default_price * 0.3)
end
def start_promotion?
calculate_range
#max_discount <= product.current_price && product.current_price <= #min_discount && Date.today.yday - product.initial_date >= 30
end
def promotion_duration
current_date = Date.today.yday
#duration = current_date - #promotion_start
end
end
Rspec
This doesn't work:
describe Product do
let(:shoes) { Product.new("shoes", "item.png", 100) }
it 'should change the photo_src and promotion attribute if applicable' do
allow(shoes).to receive(:initial_date) { 100 }
shoes.price_change(75)
expect(shoes.promotion).to eq(true)
expect(shoes.photo_src).to eq("redX.png")
end
end
This does:
describe Product do
let(:shoes) { Product.new("shoes", "item.png", 100) }
let(:calculator) { RedPencilCalculator.new(shoes) }
it 'should change the photo_src and promotion attribute if applicable' do
allow(shoes).to receive(:initial_date) { 100 }
shoes.price_change(75)
calculator.start_promotion!
expect(shoes.promotion).to eq(true)
expect(shoes.photo_src).to eq("redX.png")
end
end
So it seems to me that the start_promotion! method call in the price_change method just isn't working.
I don't have a specific answer to your bug but some suggestions on how to pinpoint the problem.
You're testing too much in one unit test. There's so much that can go wrong it's hard (as you've found) to track down where the bug lies. Even if you work it out now, when something changes down the track (as it inevitably will) it will be at least as difficult as it is now to debug.
Simplify the initializer. It should only set #name, #photo, #price. The other instance variables should be methods (write tests unless they're private).
You suspect RedPencilCalculator#start_promotion! has a bug. Write a test to eliminate that possibility.
With more tests in place, the bug will eventually be cornered and crushed!
Lastly - this is easier said than done - but try writing tests first. It is hard but gets easier and even enjoyable!
ok, I put a puts inside of start_promotion? like this:
p "got past calc range: #{#max_discount.inspect} and #{#min_discount.inspect} and #{product.current_price}"
and got:
"got past calc range: 70.0 and 95.0 and 100"
given that the following line checks that current-price is less than the min-discount...
that's the line you've gotta check/fix to make things work
I'm trying to multithread loop in ruby following this exmaple: http://t-a-w.blogspot.com/2010/05/very-simple-parallelization-with-ruby.html.
I copied that coded and wrote this:
module Enumerable
def ignore_exception
begin
yield
rescue Exception => e
STDERR.puts e.message
end
end
def in_parallel(n)
t_queue = Queue.new
threads = (1..n).map {
Thread.new{
while x = t_queue.deq
ignore_exception{ yield(x[0]) }
end
}
}
each{|x| t_queue << [x]}
n.times{ t_queue << nil }
threads.each{|t|
t.join
unless t[:out].nil?
puts t[:out]
end
}
end
end
ids.in_parallel(10){ |id|
conn = open_conn(loc)
out = conn.getData(id)
Thread.current[:out] = out
}
The way I understand it is that it will dequeue 10 items at a time, process the block in the loop per id and join the 10 threads at the end, and repeat until finished. After running this code I get different results sometimes, especially if the size of my ids is less then 10, and I am confused why this is occuring. Half the time it will not output anything for upto half the ids, even though I can check on server side that output for those ids exists. For example if the correct output is "Got id 1" and "Got id 2", it will only print out {"Got id 1"} or {"Got id 2"} or {"Got id 1", "Got id 2"}. My question is that is that is my understanding of this code correct?
The issue in my code was the open_conn() function call, which was not thread safe. I fixed the issue by synchronizing around getting the connection handle:
connLock = Mutex.new
ids.in_parallel(10){ |id|
conn = nil
connLock.synchronize {
conn = open_conn(loc)
}
out = conn.getData(id)
Thread.current[:out] = out
}
Also should use http://peach.rubyforge.org/ for the loop parallelization by using:
ids.peach(10){ |id| ... }