我试图通过定义条件方法来重构我的代码 当前代码功能完美,但我需要彻底重构它,同时保持现有的结构和可读性。
在这种情况下,要求用户进行选择。该选择与散列数组中的符号相关联。该方法应替换下面示例中的最后两个puts
。代码一遍又一遍地打印这两行。
puts"Red, white, or something else?"
user_category_selection = gets.chomp.downcase
puts "-"*80
wine_rack = [
{ name: "The Fugitive",
vintage: 2010,
category: "Red",
grape_composition: "Red Blend",
location: "Napa, California",
personal_rating: 91},
{ name: "Au Bon Climat",
vintage: 2010,
category: "White",
grape_composition: "Chardonnay",
location: "Santa Barbara, California",
personal_rating: 89},
{ name: "Laurent-Perrier",
vintage: "Non-vintage",
category: "Something Else",
grape_composition: "Ultra Brut",
location: "France",
personal_rating: 92}
]
这是不起作用的作品:
def vintage_name_location(category)
category = wine_rack[:category].downcase
while category.downcase.include? user_category_selection
puts "#{wine[:vintage]} #{wine[:name]}, #{wine[:location]}".center(80)
puts "#{wine[:grape_composition]}, rated #{wine[:personal_rating]} points".center(80)
end
end
puts vintage_name_location(user_category_selection)
其余代码是:
until ["white", "red", "something else"].include? user_category_selection.downcase
puts "Pardon me for asking, but are you intoxicated already?".center(80)
puts "I said red, white, or something else.".center(80)
user_category_selection = gets.chomp
end
if user_category_selection.downcase.eql? "red"
puts "May I suggest one of these delightful reds:"
puts "--------------------------------------------------".center(80)
for wine in wine_rack
if wine[:category].downcase == user_category_selection
puts "#{wine[:vintage]} #{wine[:name]}, #{wine[:location]}".center(80)
puts "#{wine[:grape_composition]}, rated #{wine[:personal_rating]} points".center(80)
puts "--------------------------------------------------".center(80)
end
end
elsif user_category_selection.downcase.eql? "white"
答案 0 :(得分:1)
正如您所描述的那样,您的matching_wines = wine_rack.select { |wine| wine[:category].downcase == category }
是一个数组,因此您无法使用以下类别的名称查找项目:def wines_in_category(category)
wine_rack.select { |wine| wine[:category].downcase == category }
end
。
一种方法是使用Fixed-Point Format Specifier来挑选与此类别匹配的葡萄酒:
suitable_wines = wines_in_category(user_category_selection)
for wine in suitable_wines
...
你可以在你的函数中包装它:
@Override
protected void onStart() {
super.onStart();
setEnabledAuthProvider(AuthProviderType.PASSWORD);
}
然后像这样使用它:
{{1}}
答案 1 :(得分:0)
这是另一种解决方案,它指出了你必须使用的变量。
请注意,您无法直接从该方法与wine_rack
进行互动。您可以使用send
方法打开范围门,或者只需传入wine_rack
数组。
此外,Array#select
将返回块返回true的所有实例。这可能不会立即影响您的软件,但可能会在以后导致问题:
puts "Variables I have to work with: #{local_variables}"
def get_wine_information(passed_wine_rack,user_selection)
puts "Variables I have to work with inside method: #{local_variables}"
selection = passed_wine_rack.find {|wine| wine[:category] == user_selection}
puts "#{selection[:vintage]} #{selection[:name]}, #{selection[:location]}"
puts "#{selection[:grape_composition]}, rated #{selection[:personal_rating]} points"
end
#Scope Gate
Kernel.send(:define_method, :get_wine_info) do |user_selection|
puts "Variables I have to work with inside scope gate: #{local_variables}"
end
#Calling just to see scope information
get_wine_info("")
get_wine_information(wine_rack,user_category_selection)