Skip to content

Instantly share code, notes, and snippets.

@relaxedtomato
Forked from joshuastr/candidates.rb
Last active April 29, 2016 00:46
Show Gist options
  • Save relaxedtomato/54914515979a8f70c106e79d00d7e885 to your computer and use it in GitHub Desktop.
Save relaxedtomato/54914515979a8f70c106e79d00d7e885 to your computer and use it in GitHub Desktop.
Candidates
require 'active_support/all'
@candidates = [
{
id: 5,
years_of_experience: 4,
github_points: 293,
languages: ['C', 'Ruby', 'Python', 'Clojure'],
date_applied: 5.days.ago.to_date,
age: 26
},
{
id: 7,
years_of_experience: 1,
github_points: 145,
languages: ['JavaScript', 'Ruby', 'Go', 'Erlang'],
date_applied: 15.days.ago.to_date,
age: 19
},
{
id: 9,
years_of_experience: 6,
github_points: 435,
languages: ['JavaScript', 'SQL', 'C#'],
date_applied: 1.day.ago.to_date,
age: 32
},
{
id: 10,
years_of_experience: 3,
github_points: 232,
languages: ['Java', 'Ruby', 'JavaScript'],
date_applied: 12.days.ago.to_date,
age: 31
},
{
id: 11,
years_of_experience: 12,
github_points: 32,
languages: ['VB', 'Cobol', 'Fortran'],
date_applied: 2.days.ago.to_date,
age: 42
},
{
id: 13,
years_of_experience: 2,
github_points: 328,
languages: ['Python', 'Ruby', 'JavaScript'],
date_applied: 4.days.ago.to_date,
age: 25
},
{
id: 15,
years_of_experience: 1,
github_points: 400,
languages: ['JavaScript', 'Ruby'],
date_applied: 3.days.ago.to_date,
age: 16
},
]
# In this file we define the methods to help filter out candidates
# This way, we keep these methods separated from other potential parts of the program
# def repl1
# flag = true
# while flag
# puts "Please select an option: find(user ID), all ,qualified ,quit"
# input = gets.chomp.downcase
# case input
# when /^find (\d+)$/
# pp find($1.to_i)
# when /all/
# pp @candidates
# when /qualified/
# pp order_by_qualifications(@candidates)
# when /quit/
# flag = false
# end
# end
def find(id)
@candidates.detect{|candidate| candidate[:id] == id}
end
## R.B - Good use of default params
def ordered_by_qualifications(candidates = [])
candidates.sort_by do |item|
[-item[:years_of_experience],-item[:github_points]] #-item = reverse
end
end
## R.B - General a good practice to define methods before you use them (so experienced? before hand)
def qualified_candidates(candidates = [])
candidates.select do |candidate|
experienced?(candidate) &&
github_points?(candidate) &&
can_code?(candidate) &&
applied_recently?(candidate) &&
is_adult?(candidate)
end
end
def experienced?(candidate)
candidate[:years_of_experience] >= 2
# (same as) if candidate[:years_of_experience] >= 2
# true
# else
# false
# end
end
def github_points?(candidate)
candidate[:github_points] >= 100
end
def can_code?(candidate)
candidate[:languages].include?('Ruby' || 'Python')
end
## R.B Instead of 0.days.ago.to_date, you can use Time.now()
def applied_recently?(candidate)
apply = candidate[:date_applied] - 0.days.ago.to_date <= 15
end
def is_adult?(candidate)
candidate[:age] >= 18
end
# This is the main entrypoint into the program
# It requires the other files/gems that it needs
require 'pry'
require './candidates'
require './filters'
require 'pp'
## Your test code can go here
puts '-'*4 + "all should be true" + '-' * 4
#puts experienced?[@candidates[0]) == true
user_input = ARGV[0]
while user_input != "quit"
puts case user_input
when "find"
find(ARGV[1].to_i)
when "all"
ordered_by_qualifications(@candidates)
when "qualified"
qualified_candidates(@candidates)
end
puts "enter a command"
user_input = STDIN.gets.chomp
end
puts user_input
# puts ordered_by_qualifications
#experienced?(@candidates[0])
#experienced?(@candidates[1])
# binding.pry
#pp qualified_candidates(@candidates)
## Your test code can go here
# puts '-'*4 + "ALL SHOULD BE TRUE" + '-'*4
# puts experienced?(@candidates[0]) == true
# puts experienced?(@candidates[1]) == false
# puts github_points?(@candidates[0]) == true
# puts github_points?(@candidates[1]) == true
# puts can_code?(@candidates[0]) == true
# puts can_code?(@candidates[1]) == true
# puts applied?(@candidates[0]) == true
# puts applied?(@candidates[1]) == true
# puts is_adult?(@candidates[0]) == true
# puts is_adult?(@candidates[1]) == true
# puts '-'*4 + 'FILTER PROCESS' + '-'*4
# ​
# pp qualified_candidates(@candidates)
# pp ordered_by_qualifications(@candidates)
# ​
# pp repl1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment