Skip to content

Instantly share code, notes, and snippets.

@Ninjex
Created December 17, 2014 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ninjex/a9b0eb3c503205f48cdb to your computer and use it in GitHub Desktop.
Save Ninjex/a9b0eb3c503205f48cdb to your computer and use it in GitHub Desktop.
Class Employee Example
#!/usr/bin/ruby
# Find users in the Employee hash by using their last name + first name (needs to be unique)
class Employee
attr_reader :f_name, :l_name, :email, :phone
EMPLOYEE = {}
AREA_CODE = 555 # This is just to generate number in a specific area code
PROMPT = '> '
def initialize(f_name, l_name, email, phone)
@f_name = f_name
@l_name = l_name
@email = email
@phone = phone
end
def get_id
@id = EMPLOYEE.count
end
def generate_number
number = "#{AREA_CODE}-#{rand.to_s[2..8].insert(3, '-')}"
end
def add
EMPLOYEE[@l_name + @f_name] = {:first => @f_name,
:last => @l_name,
:email => @email,
:phone => @phone,
:id => get_id
}
end
def info
puts "First Name: #{EMPLOYEE[@l_name + @f_name][:first]}"
puts "Last Name: #{EMPLOYEE[@l_name + @f_name][:last]}"
puts "Email Address: #{EMPLOYEE[@l_name + @f_name][:email]}"
puts "Phone Number: #{EMPLOYEE[@l_name + @f_name][:phone]}"
puts "ID: #{EMPLOYEE[@l_name + @f_name][:id]}"
end
def write_files
# This would write specific details to specified files
# For instance, you could make one file (employees.txt) with all the user's information
# In addition to writting the data to seperate files (f_name.txt, l_name.txt, etc)
end
# If you want to explicitly add users
# Of course we could make a script to add users from other files
def self.add_user
puts "First Name:"
print PROMPT
f_name = gets.chomp
puts "Last Name:"
print PROMPT
l_name = gets.chomp
puts "Email Address:"
print PROMPT
email = gets.chomp
puts "Phone Number:"
print PROMPT
phone = gets.chomp
EMPLOYEE[l_name + f_name] = {:first => f_name,
:last => l_name,
:email => email,
:phone => phone,
:id => EMPLOYEE.count
}
end
def remove_user
# Here you could have code to remove a user
# It could be as simple as dropping the user from the EMPLOYEE hash via their user ID
end
def self.find_user
puts "Enter the users last name combined with the first name."
puts "For example, user John Deer's information would be found with: DeerJohn"
print PROMPT
query = gets.chomp
puts "First Name: #{EMPLOYEE[query][:first]}"
puts "Last Name: #{EMPLOYEE[query][:last]}"
puts "Email Address: #{EMPLOYEE[query][:email]}"
puts "Phone Number: #{EMPLOYEE[query][:phone]}"
puts "ID: #{EMPLOYEE[query][:id]}"
end
#If you want to add a new user
add_user
#Find information on a user
find_user
end
ninjex = Employee.new('Ninjex', 'L33T', 'ninjex@tormail.org', '911')
ninjex.add # This could be done automagically, we don't explicitly need to call it
ninjex.info
limdis = Employee.new('limdis', 'F0x', 'limdis@hts.org', '911')
limdis.add
limdis.info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment