Created
April 25, 2017 22:27
-
-
Save gmmowry/f57aaaca7e0458da7225b21fab76d973 to your computer and use it in GitHub Desktop.
CJ's vampire with comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# build the driver to get the class arguments to pass into initialize | |
#takes in arguments to flesh out the instance. | |
#1.collect values from user | |
#2.call methods to evaluate at the beginning of vampire evaluate method | |
#to be better in line with best practice | |
class VampireIntake | |
attr_accessor :age, :name, :year_born, :allergy, :vampire, :insurance, :garlic, :allergies | |
# I would rename your return variables for your evaluation methods so they are not named the exact same as your instance variables | |
# there's no need for these to be written outside of the class since you're getting them from the user in the driver code | |
# Someone shouldn't be able to change these variables after instantiation | |
# You may want to keep them as attr_readers if you want to be able to read them outside the class though | |
@@current_year = 2017 #how do I use this?? | |
# instead of doing this, I would use your @year instance variable, there's no need to use a class variable | |
# Check out the Ruby class for Date - instead of hardcoding to 2017 you can dynmically change it based on the current date | |
attr_reader :year,:current_year | |
# if you don't need to read something outside the class, it doesn't need to have to have an attr_reader | |
def initialize(name,age,year_born,garlic,insurance) | |
@name = name | |
@age = age | |
@year_born = year_born | |
@garlic = garlic | |
@insurance = insurance | |
# allergies isn't getting passed in as a parameter, how are you getting the value? | |
@allergies = allergies | |
@year = 2017 | |
@vampire = 'undetermined' | |
end | |
def garlic_eval | |
# garlic = '' | |
# you can easily refactor this into a quick one line code | |
# wants_garlic = @garlic == "y" | |
# and then return wants_garlic which will either be true or false | |
# avoid naming local variables the same as instance variables, it can be confusing to read | |
# and understand which one you're accessing | |
if garlic == "y" | |
@garlic = true | |
else @garlic = false | |
end | |
p "garlic #{garlic}" | |
p @garlic | |
end | |
def insurance_eval | |
# the same refactor from garlic_eval would apply here | |
# make sure you return the actual variable not a string so you can use it properly | |
if insurance == "y" | |
@insurance = true | |
else | |
@insurance = false | |
end | |
p "insurance #{insurance}" | |
end | |
def allergy_eval #if this is sunshine, how do I stop all other loops? vampire_eval? | |
@allergies = allergy # this can't work | |
# you're not passing in anything about allergies on instantiation | |
# the logic about stopping the loop on "done" or "sunshine" would need to be done in the driver code | |
# you could have all of the allergies a user enters put into an array | |
# if that array contains the string 'sunshine' then @vampire would be set, if not, you don't need to set it | |
if allergies == 'sunshine' | |
@vampire = 'probably a vampire' | |
else | |
# this will throw your logic off since there is no local variable called vampire | |
# you don't always need an else statement in an if statement, so it would look like: | |
# def allergy_eval | |
# if @allergies.include?('sunshine') | |
# @vampire = 'probably a vampire' | |
# end | |
# @vampire | |
# end | |
@vampire = vampire | |
end | |
p @vampire | |
end | |
def real_age | |
# same refactor as the other evaluator methods | |
if year_born + age != year | |
real_age = false | |
else | |
real_age = true | |
end | |
end | |
def vampire_evaluate | |
p "real_age #{real_age}, garlic #{garlic}, insurance #{insurance}, allergy #{allergy}" | |
if real_age && (garlic && insurance) | |
@vampire = "Probably not a vampire." | |
end | |
# age = f / garlic = t / insurance = f **OR** | |
# age = f / garlic = f / insurance = t | |
if real_age == false && (( garlic && !insurance ) || (!garlic && insurance)) | |
@vampire = "Probably a vampire." | |
end | |
if real_age == false && (!garlic && !insurance) | |
@vampire = "Almost certainly a vampire" | |
end | |
if name == "Drake Cula" || name == "Tu Fang" | |
@vampire = "Definitely a vampire" | |
end | |
p @vampire | |
end | |
end #end of class | |
# puts 'how many employees do you want to process?' | |
employee_count = gets.chomp.to_i | |
employees = [] | |
allergy = '' | |
employee_count.times do | |
puts 'what is your name' | |
name = gets.chomp.to_s | |
puts 'what is your age?' | |
age = gets.chomp.to_i | |
puts 'what year were you born?' | |
year_born = gets.chomp.to_i | |
puts 'Our company cafeteria serves garlic bread. Should we order some for you? (y/n)' | |
garlic = gets.chomp | |
puts 'Would you like to enroll in the company’s health insurance? (y/n)' | |
insurance = gets.chomp.to_s | |
# allergy | |
until allergy == 'done' || allergy == 'sunshine' | |
puts 'What are your allergies? when done put done' | |
allergy = gets.chomp.to_s | |
end | |
# x = VampireIntake(name,age,year_born,garlic,insurance) | |
#?how do I instanciate with the variables just gotten? | |
# you need to call VampireIntake.new(name, age, year_born, garlic, insurance, allergies) | |
# from my comments above, make sure you pass in allergies to the class and I recommend making it an array | |
employees << x | |
# x.vampire_evaluate | |
# p x.vampire | |
end | |
p employees | |
# how would I see all the instanciations | |
# you need to iterate over the employees array and you could return the instances result something like | |
# employees.each do |person| | |
# p person.vampire | |
# end | |
# why is it returning the number of employees |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment