Reputation: 79
I want this:
class Employees
def initialize
attr_accessor :first_name, :last_name, :class
end
person = Hash.new()
person["Lauree Brown"] = "Terminated"
person["Christie Gregour"] = "Active Employee"
person["Ryan Dunn"] = "Active Employee"
to return the names of the employees with the employee status. Do I just call the Hash, or do I have to specifically call the employees? This is the full code:
class Employees
def initialize
attr_accessor :first_name, :last_name, :class
end
person = Hash.new()
person["Lauree Brown"] = "Terminated"
person["Christie Gregour"] = "Active Employee"
person["Ryan Dunn"] = "Active Employee"
if person = "Terminated"
puts "#{} is a terminated employee."
else
puts "#{} is an active employee."
end
end
As of right now, it returns this:
is terminated employee.
=> nil
Upvotes: 0
Views: 57
Reputation: 81
You can use this code.
person = Hash.new()
person["Lauree Brown"] = "Terminated"
person["Christie Gregour"] = "Active Employee"
person["Ryan Dunn"] = "Active Employee"
person.each do |key, value|
if value == "Terminated"
puts "#{key} is a terminated employee."
else
puts "#{key} is an active employee."
end
end
Upvotes: 2
Reputation: 211740
Here's an attempt at fixing it and making it more Ruby-like:
class Employee
attr_accessor :first_name, :last_name, :status
def initialize(first_name, last_name, status)
@first_name = first_name
@last_name = last_name
@status = status
end
def to_s
"%s %s is an %s Employee" % [
first_name,
last_name,
status
]
end
end
people = [ ]
people << Employee.new('Lauree', 'Brown', 'Terminated')
people << Employee.new('Christie', 'Gregour', 'Active')
people << Employee.new('Ryan', 'Dunn', 'Active')
people.each do |person|
puts person.to_s
end
It's unclear why you declared a hash, or why you were using attr_accessor
inside initialize
, a place it won't even work, it's not an instance method.
It's also worth noting to pay particular attention to =
meaning assignment and ==
meaning comparison. In your example you were assigning, so the first condition would always trigger, and would also over-write any prior data. This was probably not your intent.
Upvotes: 2