如何在ruby中编写方法(常用方法)

时间:2014-02-27 12:49:36

标签: ruby-on-rails ruby methods rake-task

我是写ruby文件的新人

  

我在ruby脚本中编写了一个任务,但在该任务中有很多重复的代码。所以我需要为此写一个通用的方法。

这是我在ruby文件中的任务

    install_projects = [ 
  'Cooperativa.Hermes.Application.Host.Console', 
  'Cooperativa.Hermes.Denormalizer.Host.Console', 
  'Cooperativa.Hermes.WebApi.Host.Console',
]

setup_projects = [ 
  'Cooperativa.Hermes.Setup.Console' 
]

replay_projects = [
  'Cooperativa.Hermes.EventReplayer.Console'
]

task :restore do

 install_projects.each do |project|
    restore_folder = "#{install}/#{project}"
    FileUtils.rm_rf restore_folder
    FileUtils.cp_r("#{backup_folder}/#{project}", restore_folder)
  end  
  setup_projects.each do |project|
    restore_folder = "#{install}/#{project}"
    FileUtils.rm_rf restore_folder
    FileUtils.cp_r("#{backup_folder}/#{project}", restore_folder)
  end   
  replay_projects.each do |project|
    restore_folder = "#{install}/#{project}"
    FileUtils.rm_rf restore_folder
    FileUtils.cp_r("#{backup_folder}/#{project}", restore_folder)
  end    
end

 restore_folder = "#{install}/#{project}"
        FileUtils.rm_rf restore_folder
        FileUtils.cp_r("#{backup_folder}/#{project}", restore_folder)

上面的代码是重复的代码。

  

我需要重新考虑一个单独的方法或常用方法。我怎样才能做到这一点?你知道吗?

如何在ruby脚本中为我的任务编写方法?

2 个答案:

答案 0 :(得分:2)

你可以:

#refactor the proc

anonymous = lambda do |project|
  restore_folder = "#{install}/#{project}"
  FileUtils.rm_rf restore_folder
  FileUtils.cp_r("#{backup_folder}/#{project}", restore_folder)
end

install_projects.each &anonymous

或者一次循环所有内容:

( install_projects + setup_projects + replay_projects).each do |project|

否则,如何将所有内容提取到一个可以拥有自己的方法和变量的类中呢?

class RestoreData

  attr_reader :install, :backup_root

  def initialize(install, backup_root)
    @install     = install
    @backup_root = backup_root
  end

  def call
    projects.each do |project|
      clean_project_folder(project)
      restore_backup(project)
    end   
  end

  private

  def clean_project_folder(project)
    FileUtils.rm_rf restore_folder(project)
  end

  def restore_backup(project)
    FileUtils.cp_r(backup_folder(project), restore_folder(project))
  end

  def backup_folder(project)
    "#{backup_root}/#{project}"
  end

  def restore_folder(project)
    "#{install}/#{project}"
  end

  def projects
    install_projects + setup_projects + replay_projects
  end

  def install_projects
    [
      'Cooperativa.Hermes.Application.Host.Console', 
      'Cooperativa.Hermes.Denormalizer.Host.Console', 
      'Cooperativa.Hermes.WebApi.Host.Console',
    ]
  end

  def setup_projects
    [ 
      'Cooperativa.Hermes.Setup.Console' 
    ]
  end

  def replay_projects
    [
      'Cooperativa.Hermes.EventReplayer.Console'
    ]
  end
end

和你的佣金任务:

task :restore do
  RestoreData.new(install, backup_folder).call
end

答案 1 :(得分:1)

这是使用方法:

def do_restore(project, install, backup_folder)
  restore_folder = "#{install}/#{project}"
  FileUtils.rm_rf restore_folder
  FileUtils.cp_r("#{backup_folder}/#{project}", restore_folder)
end

task :restore do

  install_projects.each { |project| do_restore(project, install, backup_folder) }
  setup_projects.each { |project| do_restore(project, install, backup_folder) }
  replay_projects.each { |project| do_restore(project, install, backup_folder) }

end

虽然我认为这可能是一个更好的解决方案(正如@apneadiving所建议的那样):

(install_projects + setup_projects + replay_projects).each do |project|
  restore_folder = "#{install}/#{project}"
  FileUtils.rm_rf restore_folder
  FileUtils.cp_r("#{backup_folder}/#{project}", restore_folder)
end

由于您实际上是在迭代三个数组,执行相同的操作,这就像您将三个数组一起添加,并在连接的数组上进行迭代一样。要将两个数组连接在一起,您可以使用+运算符 - [1,2]+[3,4]==[1,2,3,4],因此install_projects + setup_projects + replay_projects只是一长串项目列表,连接在一起:

install_projects = [ 
  'Cooperativa.Hermes.Application.Host.Console', 
  'Cooperativa.Hermes.Denormalizer.Host.Console', 
  'Cooperativa.Hermes.WebApi.Host.Console',
]

setup_projects = [ 
  'Cooperativa.Hermes.Setup.Console' 
]

replay_projects = [
  'Cooperativa.Hermes.EventReplayer.Console'
]

(install_projects + setup_projects + replay_projects) == [
  'Cooperativa.Hermes.Application.Host.Console', 
  'Cooperativa.Hermes.Denormalizer.Host.Console', 
  'Cooperativa.Hermes.WebApi.Host.Console',
  'Cooperativa.Hermes.Setup.Console',
  'Cooperativa.Hermes.EventReplayer.Console'
]