Ruby 是否可以在不使用全局变量的情况下对此进行优化?
我最近开始学习ruby,我尽量避免使用全局变量。我编写了下面的程序,它接受用户输入并输出用户选择的数学表(目前只有+,*但有待扩展)。我正在按照来自的建议来学习Ruby 是否可以在不使用全局变量的情况下对此进行优化?,ruby,global-variables,Ruby,Global Variables,我最近开始学习ruby,我尽量避免使用全局变量。我编写了下面的程序,它接受用户输入并输出用户选择的数学表(目前只有+,*但有待扩展)。我正在按照来自的建议来学习 class User_input . # multiply def User_input.mult1_to_12 by = (0..12).each do | range | result = $choice_int * range puts "#{$choice
class User_input
.
# multiply
def User_input.mult1_to_12
by = (0..12).each do | range |
result = $choice_int * range
puts "#{$choice_int} x #{range} = #{result}"
end
end
# add
def User_input.add1_to_12
add = (0..12).each do | range |
result = $choice_int + range
puts "#{$choice_int} + #{range} = #{result}"
end
end
# accepts user input
puts "Please enter the tables you require (1-12): "
$choice_int = gets.to_i
puts "You have selected #{$choice_int}"
puts "Which tables do you require (+ - * /): "
choice_method = gets.chomp
puts "the method you have chosen is #{choice_method}"
if choice_method == "*"
User_input.mult1_to_12
elsif
choice_method == "+"
add1_to_12
end
end
您会注意到,我目前正在为
$choice
使用一个全局变量。有经验的人能提出一个更好的解决方案吗。请随意撕开我的代码:)谢谢。方法可以接受参数,例如:
# add numbers
def add(a,b)
a+b
end
puts add(1,2)
# will output 3
下面是使用参数对代码的简单修改:
class UserInput
# multiply
def self.mult1_to_12(choice_int)
(0..12).each do | range |
result = choice_int * range
puts "#{choice_int} x #{range} = #{result}"
end
end
# add
def self.add1_to_12(choice_int)
(0..12).each do | range |
result = choice_int + range
puts "#{choice_int} + #{range} = #{result}"
end
end
end
# accepts user input
puts "Please enter the tables you require (1-12): "
choice_int = gets.to_i
puts "You have selected #{choice_int}"
puts "Which tables do you require (+ - * /): "
choice_method = gets.chomp
puts "the method you have chosen is #{choice_method}"
if choice_method == "*"
UserInput.mult1_to_12(choice_int)
elsif choice_method == "+"
UserInput.add1_to_12(choice_int)
end
这里有一个更漂亮的解决方案,它还可以处理和/(以及Ruby的Fixnum提供的一系列其他操作):
您的问题更适合SO的姐妹站点,在那里您可以获得关于如何改进工作代码的有用建议。SO的目的是为修复损坏的代码提供帮助,并为如何完成给定任务提供建议。此外,在代码评审时,您将倾向于获得更详细的解释,说明以特定方式处理问题的原因。@CarySwoveland感谢Cary提供的提示。我们来看一下代码评审。“这能以一种更优化的方式完成吗?”这意味着您的代码已经是优化的,因此根据定义,它无法得到改进。(-):和|-:。只用于工作代码,而这一行不是。即使不仔细检查它,更不用说运行它,我也可以通过浏览它至少发现两个错误。第2行有一个明显的
SyntaxError
,第三行到最后一行有一个语义错误。因为我们在这里:调用new
,而不使用arguments,然后使用setters设置对象是一种代码气味。对象在构造后应该是有效的,但在您的情况下,UserInputProcessor
实例将爆炸NoMethodError
s,表示nil
,如果您试图在构造后直接使用它。还有一个小实例:put
已经通过逐行打印元素来专门处理Array
s,不需要在最后一行调用join
。to_i
忽略字符串开头数字后的所有内容,不需要chomp
它。
class UserInputProcessor
# define accessors to instance variables
attr_accessor :choice_int, :choice_method
def process
(0..12).map do |range|
if choice_method.eql?('/')
next if range.eql?(0) # need to skip X/0 to avoid division by zero
range = range.to_f # need to convert range to float to get float results
end
"#{choice_int} #{choice_method} #{range.to_i} = #{choice_int.send(choice_method, range)}"
end
end
end
handler = UserInputProcessor.new
print "Please enter the tables you require (1-12): "
handler.choice_int = gets.chomp.to_i
puts "You have selected #{handler.choice_int}"
print "Which tables do you require (+ - * /): "
handler.choice_method = gets.chomp
puts "the method you have chosen is #{handler.choice_method}"
puts "And here are the results:"
puts handler.process.join("\n")