Ruby 降低方法的代码复杂度
实际上,我正在使用此方法替换文件的给定内容:Ruby 降低方法的代码复杂度,ruby,Ruby,实际上,我正在使用此方法替换文件的给定内容: def self.fix_authorgroup_work(title, name, email_business, company_name, company_division) agroup = "#{title}/de-DE/Author_Group.xml" namechomp = name.chomp # @note Split the variable to the array title[*] name
def self.fix_authorgroup_work(title, name, email_business, company_name, company_division)
agroup = "#{title}/de-DE/Author_Group.xml"
namechomp = name.chomp
# @note Split the variable to the array title[*]
name = namechomp.split(' ')
firstname = name[0]
surname = name[1]
# @note Author Group: Change the default stuff to the present user
puts 'Replace the default content with the new content from the user (Authors_Group)'.color(:yellow)
text = File.read(agroup)
vorname = text.gsub('Enter your first name here.', "#{firstname}")
puts vorname
File.open(agroup, 'w') { |file|
file.puts vorname
}
text = File.read(agroup)
nachname = text.gsub('Enter your surname here.', "#{surname}")
puts nachname
File.open(agroup, 'w') { |file|
file.puts nachname
}
text = File.read(agroup)
email = text.gsub('Enter your email address here.', "#{email_business}")
puts email
File.open(agroup, 'w') { |file|
file.puts email
}
text = File.read(agroup)
member = text.gsub('Initial creation by publican', 'Initial creation')
puts member
File.open(agroup, 'w') { |file|
file.puts member
}
text = File.read(agroup)
org = text.gsub('Enter your organisation\'s name here.', "#{company_name}")
puts org
File.open(agroup, 'w') { |file|
file.puts org
}
text = File.read(agroup)
div = text.gsub('Enter your organisational division here.', "#{company_division}")
puts div
File.open(agroup, 'w') { |file|
file.puts div
}
所以看起来有点复杂。也许有人能告诉我,如何降低代码的复杂性?遗憾的是,我以前没有做过这样的事 可以通过如下良好的重构来解决:
def self.add_result(nice_description, value_name, agroup)
text = File.read(agroup)
new_value = text.gsub(nice_description, value_name)
puts new_value
File.open(agroup, 'w') { |file|
file.puts new_value
}
def self.fix_authorgroup_work(title, name, email_business, company_name, company_division)
agroup = "#{title}/de-DE/Author_Group.xml"
namechomp = name.chomp
# @note Split the variable to the array title[*]
name = namechomp.split(' ')
firstname = name[0]
surname = name[1]
# @note Author Group: Change the default stuff to the present user
puts 'Replace the default content with the new content from the user (Authors_Group)'.color(:yellow)
add_result('Enter your first name here.', "#{firstname}", agroup)
add_result('Enter your surname here.', "#{surname}", agroup)
add_result('Enter your email address here.', "#{email_business}", agroup)
add_result('Initial creation by publican', "Initial creation", agroup)
add_result('Enter your organisation\'s name here.', "#{company_name}", agroup)
add_result('Enter your organisational division here.', "#{company_division}", agroup)
可以通过这样一个好的重构来解决:
def self.add_result(nice_description, value_name, agroup)
text = File.read(agroup)
new_value = text.gsub(nice_description, value_name)
puts new_value
File.open(agroup, 'w') { |file|
file.puts new_value
}
def self.fix_authorgroup_work(title, name, email_business, company_name, company_division)
agroup = "#{title}/de-DE/Author_Group.xml"
namechomp = name.chomp
# @note Split the variable to the array title[*]
name = namechomp.split(' ')
firstname = name[0]
surname = name[1]
# @note Author Group: Change the default stuff to the present user
puts 'Replace the default content with the new content from the user (Authors_Group)'.color(:yellow)
add_result('Enter your first name here.', "#{firstname}", agroup)
add_result('Enter your surname here.', "#{surname}", agroup)
add_result('Enter your email address here.', "#{email_business}", agroup)
add_result('Initial creation by publican', "Initial creation", agroup)
add_result('Enter your organisation\'s name here.', "#{company_name}", agroup)
add_result('Enter your organisational division here.', "#{company_division}", agroup)
丹尼尔·斯莱特的答案很好 此外,您还可以从以下方面清理您的namechomp业务:
namechomp = name.chomp
name = namechomp.split(' ')
firstname = name[0]
surname = name[1]
致:
丹尼尔·斯莱特的答案很好 此外,您还可以从以下方面清理您的namechomp业务:
namechomp = name.chomp
name = namechomp.split(' ')
firstname = name[0]
surname = name[1]
致:
一个好的起点是将其重新构造为。一个好的起点是将其重新构造为。如果您在ruby中使用多行块,请使用
do end
而不是{}
如果您在ruby中使用多行块,请使用do end
而不是{/code>Wooow。它看起来比我的好很多步。非常感谢:-)哇。它看起来比我的好很多步。非常感谢:-)