如何减少Ruby上的if条件?

如何减少Ruby上的if条件?,ruby,Ruby,在这个程序中,我在硬盘上查找一个文件,按名称、扩展名、名称和扩展名进行选择,直到代码变大,我想知道如何将其缩小一点。 如何减少Ruby中if条件的行数? 在Ruby中,减少以下情况的最佳方法是什么 require "colorize" new_name_file = {} loop do puts puts "Will we search for the file by name(1), extension(2), name extension(3) or exit pr

在这个程序中,我在硬盘上查找一个文件,按名称、扩展名、名称和扩展名进行选择,直到代码变大,我想知道如何将其缩小一点。 如何减少Ruby中if条件的行数? 在Ruby中,减少以下情况的最佳方法是什么

require "colorize"

new_name_file = {}

loop do

    puts 
    puts "Will we search for the file by name(1), extension(2), name extension(3) or exit programm(4)?"
    print "\n>>>>>> ".magenta
    name_extension = gets.to_i

    if name_extension == 1  # =========== search file name =================
        puts "Enter file name (test, lesson, ruby....etc.) "
        print "\n>>>>>> ".cyan

        file_name = gets.strip.downcase


        puts "Name the hard drive on which we will search for the file(C, D, E, F....e.t.c.): "
        print "\n>>>>>> ".green
        hdd_search = gets.strip.capitalize

        # search file hdd
        contents = Dir.glob("#{hdd_search}:/**/#{file_name}.*")

    elsif name_extension == 2 # ========= search by file extension =============

        puts "Enter file extension(txt, rb, jpg, csv, json) "
        print "\n>>>>>> ".cyan
        file_extension = gets.strip.downcase

        # on which drive we will search put the letter
        puts "Name the hard drive on which we will search for the file(C, D, E, F....e.t.c.): "
        print "\n>>>>>> ".green
        hdd_search = gets.strip.capitalize


        # search file hdd
        contents = Dir.glob("#{hdd_search}:/**/*.#{file_extension}") 

    elsif name_extension == 3 # ========= search by name and file extension =============

        puts "Enter a name and file extension(test.txt, test.rb, test.jpg, test.csv, test.json..etc) "
        print "\n>>>>>> ".cyan
        file_extension_name = gets.strip

        # on which drive we will search put the letter
        puts "Name the hard drive on which we will search for the file(C, D, E, F....e.t.c.): "
        print "\n>>>>>> ".green
        hdd_search = gets.strip.capitalize

        # search file hdd   
        contents = Dir.glob("#{hdd_search}:/**/#{file_extension_name}") 

    elsif name_extension == 4
        puts "Exit programm".red
        exit

    end

    contents.each do |txt_name|
        z_name =  File.basename(txt_name)  # file name
        path = File.expand_path(txt_name)  # path file
        new_name_file[z_name] = path       # everything in the hash

    end

    new_name_file.each do |k, v|           # hash output
        puts "file : ".cyan + "#{k} " + " path:".cyan +  "#{v}"
    end

end

您可以将代码包装在方法中的条件中:

    def process_file(file_types)
        puts "Enter file name (#{file_types.join(',')}....etc.) "
        print "\n>>>>>> ".cyan

        file_name = gets.strip.downcase


        puts "Name the hard drive on which we will search for the file(C, D, E, F....e.t.c.): "
        print "\n>>>>>> ".green
        hdd_search = gets.strip.capitalize

        # search file hdd
        contents = Dir.glob("#{hdd_search}:/**/#{file_name}.*")
    end
    file_types = {
      "1" => ['test', 'lesson', 'ruby']
      "2" => 
    }

   loop do
      puts 
      puts "Will we search for the file by name(1), extension(2), name extension(3) or exit programm(4)?"
      print "\n>>>>>> ".magenta
      name_extension = gets
      if name_extension == '4'
        puts "Exit programm".red
        exit
      end

      process_file(file_types[name_extension])
   end

通过减少主循环所做的工作,并专注于获取输入和委派,您可以对这段代码进行大量Rubyize:

class Tester
  def select
    loop do
      puts 
      puts "Will we search for the file by name(1), extension(2), name extension(3) or exit programm(4)?"
      print "\n>>>>>> "

      input = gets.chomp
      search_method = "search_#{input}"

      if (respond_to?(search_method))
        contents = send(search_method)

        contents.each do |txt_name|
          z_name =  File.basename(txt_name)  # file name
          path = File.expand_path(txt_name)  # path file
          new_name_file[z_name] = path       # everything in the hash
        end
      else
        puts "Unknown input: #{input.inspect}, method #{search_method} not defined."
      end
    end
  end
end
然后,您可以使用以下方法:

Tester.new.select
这将动态地分派给遵循简单命名的方法 惯例。要添加其他方法,请定义一个:

def search_1
  puts "Enter file name (test, lesson, ruby....etc.) "
  print "\n>>>>>> ".cyan

  file_name = gets.strip.downcase


  puts "Name the hard drive on which we will search for the file(C, D, E, F....e.t.c.): "
  print "\n>>>>>> ".green
  hdd_search = gets.strip.capitalize

  # search file hdd
  Dir.glob("#{hdd_search}:/**/#{file_name}.*")
end
这些方法应返回要显示的内容。这很简单


您可以更进一步,定义类而不是方法,在这些方法中您可以实例化类并迭代它们的结果,从而使其更接近推荐值。

您忘记了
.chomp
on
name\u extension=get
。这是一件小事,但最好编写
文件类型={'1'=>“输入文件名(测试、课程、ruby…等)”,'2'=>…}
contents=Dir.glob(“{hdd\u search}://**/{file\u name}.”
您不需要更改吗?你有相同的
“#{file_name}.*”和“#{*.extension}”
我试过你的代码,但是在搜索中写什么?方法的名称不进行搜索,文件名也不希望看到该方法。我按您的方式输入了代码,输入了方法的名称,方法1“未知输入。在我看来,这里的问题似乎是(响应?(搜索方法))名称应该是
search\u 1
而不是
method\u 1
。这在类中会更好。一切都很好,我理解,您编写了代码,因为我最初是按数字搜索的,我不理解如何查看search\u 1函数,他们没有看到使用该方法的类!这是代码的位置