简化Ruby声明

时间:2016-08-19 09:58:26

标签: ruby

这里有一个很长的代码w / c是begin-end block和if语句的组合。如何在all if语句中简化这一点?

print "Put a number between 0 to 100: "

begin
numb = gets.chomp
numb = Integer(numb)
rescue 
    print "Please put a number not a text: "
    retry
end

if numb < 0
  puts "You can't enter a negative number!"
elsif numb <= 50
  puts "#{numb} is between 0 and 50"
elsif numb <= 100
  puts "#{numb} is between 51 and 100"
else
  puts "#{numb} is above 100"
end

重构

3 个答案:

答案 0 :(得分:2)

指定要挽救的例外类是一个好习惯。在这种情况下,它是一个ArgumentError。

此外,您的代码看起来还不错。 读取输入可以抽象为方法:

def read_input
  Integer(gets.chomp)
rescue ArgumentError
  print "Please put a number not a text: "
  retry
end

在代码的第二部分中,只有puts可以移到if前面,以避免在每种情况下都输入puts

print "Put a number between 0 to 100: "
numb = read_input
puts if numb < 0
  "You can't enter a negative number!"
elsif numb <= 50
  "#{numb} is between 0 and 50"
elsif numb <= 100
  "#{numb} is between 51 and 100"
else
  "#{numb} is above 100"
end

代码的这一部分也可以用范围和case语句表示,这可以说是更具可读性:

puts case numb
  when -Float::INFINITY...0 then "You can't enter a negative number!"
  when 0..50 then "#{numb} is between 0 and 50"
  when 51..100 then "#{numb} is between 51 and 100"
  else "#{numb} is above 100"
end

此外,-Float::INFINITY...0语句可由proc(&:negative?)表示。 Ruby sice 2.3版本中提供了Numeric#positive?Numeric#negative?方法。

答案 1 :(得分:0)

print 'Put a number between 0 to 100: '

begin
  numb = Integer(gets.chomp)
rescue ArgumentError
  print 'Please put a number not a text:'
  retry
end

case numb
when -Float::INFINITY...0
  'You can\'t enter a negative number!'
when 0..50
  "#{numb} is between 0 and 50"
when 51..100
  "#{numb} is between 51 and 100"
else
  "#{numb} is above 100"
end

Youl shouldn't rescue Exception

根据ruby code style,您可以更改不必要的双引号。 (看看rubocop宝石)

然后改变if else,然后改变。

答案 2 :(得分:0)

要么使用case语句,要么根据存储限制的变量实现检查的情况太多,以检查整数。

下面的代码与您的初始代码,案例陈述和存储限制的数组相同,是最重要的解决方案。

(1..7).each {
  print "Put a number between 0 to 100: "

  begin
    numb = gets.chomp
    numb = Integer(numb)
  rescue
    print "Please put a number not a text: "
    retry
  end

  if numb < 0
      puts "You can't enter a negative number!"
  elsif numb <= 50
      puts "#{numb} is between 0 and 50"
  elsif numb <= 100
      puts "#{numb} is between 51 and 100"
  else
      puts "#{numb} is above 100"
  end

  case
    when numb < 0
      puts "[case] You can't enter a negative number!"
    when numb.between?(0, 50)
      puts "[case] #{numb} is between 0 and 50"
    when numb.between?(51, 100)
      puts "[case] #{numb} is between 51 and 100"
    when numb > 100
      puts "[case] #{numb} is above 100"
  end

  lim = [100, 0, 50].sort
  if (numb < lim.first)
    puts "[limit list] You can't enter a negative number!"
  elsif (numb > lim.last)
      puts "[limit list] #{numb} is above #{lim.last}"
  else
    (lim.first lim.size - 1).each_index { |i|
      min_value = lim[i].eql?(0) ? 0 : lim[i]+1
      if numb.between?(min_value,lim[i+1])
        puts "[limit list] #{numb} is between #{min_value} and #{lim[i+1]}"
        break
      end
    }
  end
  puts
}

输出:

Put a number between 0 to 100: -3
You can't enter a negative number!
[case] You can't enter a negative number!
[limit list] You can't enter a negative number!

Put a number between 0 to 100: 0
0 is between 0 and 50
[case] 0 is between 0 and 50
[limit list] 0 is between 0 and 50

Put a number between 0 to 100: 42
42 is between 0 and 50
[case] 42 is between 0 and 50
[limit list] 42 is between 0 and 50

Put a number between 0 to 100: 50
50 is between 0 and 50
[case] 50 is between 0 and 50
[limit list] 50 is between 0 and 50

Put a number between 0 to 100: 84
84 is between 51 and 100
[case] 84 is between 51 and 100
[limit list] 84 is between 51 and 100

Put a number between 0 to 100: 100
100 is between 51 and 100
[case] 100 is between 51 and 100
[limit list] 100 is between 51 and 100

Put a number between 0 to 100: 168
168 is above 100
[case] 168 is above 100
[limit list] 168 is above 100