ruby类方法无法将其他用户输入推送到数组

时间:2017-08-02 20:51:32

标签: arrays ruby class-method

它存储第一个员工,但是当我尝试添加另一个员工时,我无法查看输入的其他员工。以下是查看员工记录的方法。谢谢你的帮助!

员工类

class Employee
  attr_accessor :employee, :role, :location

  def initialize(employee, role, location)
    @employee = employee
    @role = role
    @location = location
  end

  def employee_change(new_emp)
    @employee = new_emp
  end

  def role_change(new_role)
    @role = new_role
  end.


  def location_change(new_loc)
    @location = new_loc
  end
end

主菜单

def main_menu
  puts "Welcome to the Employee Portal"
  puts "Please select an option below: "
  puts "---------------------"
  puts "1. Create New Employee Record."
  puts "2. View an existing record."
  puts "3. Modify an existing record."
  puts "4. Exit Portal"

  option = gets.chomp.to_i

  if option == 1
    create_record
    main_menu

  elsif option == 2
    view_record

  elsif option == 3
    modify

  elsif option == 4
    puts "Thank you for using the Employee Portal"
    exit
  else
    puts "Not a valid option. Please try again."
    main_menu
    system("clear")

  end
end

创建新员工(主菜单中的选项1)

def create_record
  puts "Create New Employee Record, click 'Enter' to begin"
  puts "Enter Employee Information: "
  employee = gets.chomp.capitalize

  puts "Enter Employee's Role: "
  role = gets.chomp.capitalize

  puts "Enter Employee's Current Work Location: "
  location = gets.chomp.capitalize

  puts "\n"

  new_record = Employee.new(employee, role, location)
  @record.push(new_record)

  puts "New Employee Record has been created."
  puts "Name: #{employee}"
  puts "Role: #{role}"
  puts "Location: #{location}"

  system("clear")

  main_menu
end

查看员工(主菜单中的选项2)

def view_record
  puts "Enter Employee Name to view record: "
  name = gets.chomp.capitalize
  system("clear")

  @record.each do |a|
    if a.employee == name

      puts "\n"
      puts "Employee Information "
      puts "--------------------"
      puts " Name :  #{a.employee}"
      puts " Role(s) :  #{a.role}"
      puts " Location(s) :  #{a.location}"
      puts " Type 'Exit' to return to the Main Menu "

    else
      puts "That is not a valid entry, please try again."
      view_record
      main_menu
    end
  end
end

修改员工(主菜单中的选项3)

def modify
  system("clear")
  puts "Enter employee name to modify existing record: "
  name = gets.chomp.capitalize

  @record.each do |r|
    if r.employee == name
      puts "Employee found."
      puts "Select an option to modify."
      puts "-----------------------------------"
      puts "1.) Modify employee's name."
      puts "2.) Modify employee's role."
      puts "3.) Modify employee's location."
      puts "4.) Return to Main Menu"
      puts "\n"

      option = gets.chomp.to_i

      if option == 1
        change_employee

      elsif option == 2
        change_role

      elsif option == 3
        change_location

      elsif option == 4
        main_menu
      else
        puts "Invalid selection. Please try again."
        modify
      end
    end
  end
end

更改员工(修改选项1)

def change_employee
  puts "Enter new employee name: "
  new_emp = gets.chomp.capitalize
  @record.each do |r|
    if r.employee == employee
      r.employee_change(new_emp)
      puts "#{r.employee} has been updated to #{r.employee}"
    end
  end
end

更改员工角色(修改选项2)

def change_role
  puts "What is #{r.employee}\'s new role?: "
  new_role = gets.chomp.capitalize
  @record.each do |r|
    if r.employee == employee
      r.role_change(new_role)
      puts "#{r.employee}\'s new role is #{r.role}"
    end
  end
end

更改员工位置(修改选项3)

def change_location
  puts "What is #{r.employee}\'s new location?: "
  new_loc = gets.chomp.capitalize
  @record.each do |r|
    if r.employee == employee
      r.location_change(new_loc)
      puts "#{r.employee} has been transfer to new location, #{r.location}."
    end
  end
end

运行程序

@record = []
system("clear")
main_menu

1 个答案:

答案 0 :(得分:0)

问题在于您的view_record方法。如果你改变它看起来像:

def view_record
  puts "Enter Employee Name to view record: "
  name = gets.chomp.capitalize
  system("clear")

  if a = @record.detect { |rec| rec.employee == name }
    puts "\n"
    puts "Employee Information "
    puts "--------------------"
    puts " Name :  #{a.employee}"
    puts " Role(s) :  #{a.role}"
    puts " Location(s) :  #{a.location}"
    puts " Type 'Exit' to return to the Main Menu "
  else
    puts "That is not a valid entry, please try again."
    view_record
  end
end

它运作正常。

问题在于您在if...else中的每条记录上调用@record语句。因此,如果您创建2 Employee s,第一个名为“John”,第二个名为“Jane”。当你去查看“简”时,你将else部分称为“约翰”,因为他是第一个记录,然后一旦他完成else,你就称之为if部分简”。但是,“John”的else永远不会返回,因为在您最终完成view_recordmain_menuelse次调用之后,会出现同样的问题,然后返回main_menu永远不会返回(由于该方法中的最终modify条件重新调用change_x

希望这有帮助并且有意义。

注意:

  1. if似乎有同样的问题,else方法会循环检查并检查每个员工的detect语句,但由于没有change_x没问题(我仍然会亲自更改它们,并在这些上使用employee
  2. select方法看似不会运行,因为detect未在其中定义
  3. 如果超过1名员工可以使用相同的名称,则可以使用empty?代替select,然后检查if或仅迭代InnerFunc <- function(x, y) { x + (y^2) } InnerIntegral <- function(y) { sapply(y, function(z) { integrate(InnerFunc, 0, 2, y=z)$value }) } integrate(InnerIntegral, 0, 1) # 2.666667 with absolute error < 3e-14 返回的人员并且只调用它们上的InnerFunc部分。