Rubocop Lint:无用的变量赋值

Rubocop Lint: Useless assignment to variable

请问我的代码有问题 它在控制台中运行完美,但 linters 不断失败并出现以下错误。

这是错误

    user_name = gets.chomp
refactored.rb:14:5: W: Lint/UselessAssignment: Useless assignment to variable - user_age. Did you mean user_name?
    user_age = Integer(gets.chomp)
    ^^^^^^^^

这是代码

require './school-library/student'
require './school-library/rental'
require './school-library/persons'
require './school-library/teacher'
require './school-library/book'

class Methods
  def initialize
    @person_array = []
  end

  def user_input
    print 'Age: '
    user_age = Integer(gets.chomp)

    print 'Name: '
    user_name = gets.chomp
  end

  def create_person
    puts 'Do you want to create a student (1) or a teacher (2)? [Input the number]: '
    person_option = Integer(gets.chomp)
    case person_option
    when 1
      (user_age, user_name) = user_input

      print 'Has parent permission? [Y/N]: '
      user_permission = gets.chomp.to_s.upcase

      case user_permission
      when 'Y'
        user_permission = true
      when 'N'
        user_permission = false
      end
      student = Student.new(user_age, user_name, parent_permission: user_permission)
      @person_array.push({
                           output: "[Student] Name: #{student.name}, ID: #{student.id}, Age: #{student.age}",
                           object: student
                         })

      puts 'Person created successfully!'
      puts "\n"
    when 2
      (user_age, user_name) = user_input

      print 'Specialization: '
      user_specialization = gets.chomp

      teacher = Teachers.new(user_age, user_name, user_specialization)
      @person_array.push({
                           output: "[Teacher] Name: #{teacher.name}, ID: #{teacher.id}, Age: #{teacher.age}",
                           object: teacher
                         })

      puts 'Person created successfully!'
      puts "\n"
    else
      puts 'Person not created.'
    end
  end

  def people_list
    @person_array.each do |person|
      puts person[:output]
    end
  end

  def person_rent
    @person_array.each_with_index do |person, index|
      puts "#{index}) #{person[:output]}"
    end
  end
end

class BooksList
  def initialize
    @books = []
  end

  def create_book
    print 'Title: '
    book_title = gets.chomp

    print 'Author: '
    book_author = gets.chomp

    puts 'Book created successfully!'

    book = Book.new(book_title, book_author)
    @books.push({
                  output: "Title: #{book.title}, Author: #{book.author}",
                  object: book
                })
  end

  def book_list
    @books.each do |book|
      puts book[:output]
    end
  end

  def rent_book
    @books.each_with_index do |book, index|
      puts "#{index}) #{book[:output]}"
    end
  end
end

RuboCop 试图告诉您您的代码中存在一个无用的变量,但在修复之前我们需要讨论这里发生的错误:

(user_age, user_name) = user_input

在Ruby方法的最后一行代码returns。查看方法 user_input 我们可以看到它 returns 只有 user_name 变量。问题是 (user_age, user_name) 需要两个值。第一个正在分配,但第二个被分配 nil.

要解决此问题,请将方法 user_input 更新为:

def user_input
  print 'Age: '
  user_age = Integer(gets.chomp)

  print 'Name: '
  user_name = gets.chomp

  [user_age, user_name]
end

这将修复 RuboCop 攻击和错误。