Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up of code to match rubocop rules. #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions GemCity.rb

This file was deleted.

62 changes: 62 additions & 0 deletions gem_city.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This class represents the town of GemCity
# This is a town riddled with crime but we can find out how happy the town is
class GemCity
attr_reader :population
def initialize
@people = {
thieves: 5,
Officers: 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this hash internal such that downcasing Officers to officers won't break anything?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, what about ditching the hash for two instance variables (@thieves & @officers)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to make changes that were not based on rubocop output. But, both of these changes would definitely improve the code.

}
@population = 50
end

def thieves
@people[:thieves]
end

def thieves=(thieves_number)
@people[:thieves] = thieves_number
end

def officers
@people[:Officers]
end

def officers=(officers)
@people[:Officers] = officers
end

def happiness_of_town
# happiness is random... people don't know what they want!
happiness = 0
(1..population).each do
happiness += rand((100 - successful_crime_rate)..100)
end
happiness / 100
end

def successful_crime_rate
thieves = @people[:thieves]
officers = @people[:Officers]
odds_percent = 0
if thieves > 0 && officers <= thieves
odds = 1 - officers.to_f / thieves.to_f
odds_percent = odds * 100
end
odds_percent
end

def city_demographics
civilians = population - (thieves + officers)
{ thieves: "#{population_percentage thieves}%",
officers: "#{population_percentage officers}%",
civilians: "#{population_percentage civilians}%"
}
end

private

def population_percentage(number)
((number.to_f / population.to_f) * 100).to_i
end
end
48 changes: 24 additions & 24 deletions gem_city_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'rspec'
require_relative 'GemCity' # This line may need to be changed
require_relative 'gem_city' # This line may need to be changed

describe 'Gem City' do
let(:city) { GemCity.new }
Expand All @@ -15,19 +15,19 @@
end

it 'officers = thieves' do
city.thieves 1
city.setOfficers 1 # This line may need to be changed
city.thieves = 1
city.officers = 1 # This line may need to be changed
expect(city.successful_crime_rate).to eq(0)
end

it 'thieves = 0' do
city.thieves 0
city.thieves = 0
expect(city.successful_crime_rate).to eq(0)
end

it 'officers > thieves' do
city.thieves 1
city.setOfficers 2 # This line may need to be changed
city.thieves = 1
city.officers = 2 # This line may need to be changed
expect(city.successful_crime_rate).to eq(0)
end
end
Expand All @@ -38,26 +38,26 @@
end

it 'successful_crime_rate = 0' do
city.thieves 0
city.thieves = 0
expect(city.happiness_of_town).to eq(50)
end
end

# context 'city_demographics' do
# it 'initialize' do
# demographics = city.city_demographics
# expect(demographics[:thieves]).to eq('10%')
# expect(demographics[:officers]).to eq('2%')
# expect(demographics[:civilians]).to eq('88%')
# end

# it 'thieves = 10, officers = 25' do
# city.thieves = 10
# city.officers = 25
# demographics = city.city_demographics
# expect(demographics[:thieves]).to eq('20%')
# expect(demographics[:officers]).to eq('50%')
# expect(demographics[:civilians]).to eq('30%')
# end
# end
context 'city_demographics' do
it 'initialize' do
demographics = city.city_demographics
expect(demographics[:thieves]).to eq('10%')
expect(demographics[:officers]).to eq('2%')
expect(demographics[:civilians]).to eq('88%')
end

it 'thieves = 10, officers = 25' do
city.thieves = 10
city.officers = 25
demographics = city.city_demographics
expect(demographics[:thieves]).to eq('20%')
expect(demographics[:officers]).to eq('50%')
expect(demographics[:civilians]).to eq('30%')
end
end
end