有没有办法测试3

时间:2011-12-15 03:12:28

标签: ruby operators

在ruby中有一种方法可以执行以下操作

20.times do |n|
  if 3 < n < 10
    id = 40555
  end
end

5 个答案:

答案 0 :(得分:10)

您可以使用两种比较:

if 3 < n && n < 10

或者您可以使用between

if n.between? 3, 10

或者您可以使用范围和cover?(1.9)或include?(1.8)方法:

if (3..10).cover? n

请注意,最后两个包括端点。但是,范围可以选择性地排除其端点。

BTW,在代码审查中,我将“魔术数字”标记为3和10,因为需要将其重构为适当命名的自我记录方法,例如if within_tolerance?(n)或类似的方法。 / p>

答案 1 :(得分:6)

n.between?(3, 10)

(感谢简陋的Comparable模块)

答案 2 :(得分:2)

20.times do |n|
  if 3 < n && n < 10
    id = 40555
  end
end

答案 3 :(得分:1)

以各种实施的速度来捅是一个很好的练习。

对于jollies,我将评论中建议的代码添加到OP的问题中,这令我感到惊讶。我不会期望测试中的变化能够提高速度,但确实会产生微小的差异。要理解为什么我添加空格,以防它是一个行解析问题,我认为不会。而且,为了排除它,我添加了and&&运算符。

#!/usr/bin/env ruby

require 'benchmark'

puts "Ruby #{ RUBY_VERSION }"

num_of_loops = 1000000

5.times do
  Benchmark.bm(10) do |benchmark|
    benchmark.report('<')        { num_of_loops.times { 3 < 4 && 4 < 10    } }
    benchmark.report('and')      { num_of_loops.times { 4>3 and 4<10       } }
    benchmark.report('and space'){ num_of_loops.times { 4 > 3 and 4 < 10   } }
    benchmark.report('&&')       { num_of_loops.times { 4>3 && 4<10        } }
    benchmark.report('between?') { num_of_loops.times { 4.between? 3, 10   } }
    benchmark.report('cover?')   { num_of_loops.times { (3..10).cover? 4   } }
    benchmark.report('include?') { num_of_loops.times { (4..9).include?(4) } }
  end
  puts
end

Ruby 1.9.2
                user     system      total        real
<           0.230000   0.000000   0.230000 (  0.237798)
and         0.230000   0.000000   0.230000 (  0.220596)
and space   0.220000   0.000000   0.220000 (  0.221239)
&&          0.220000   0.000000   0.220000 (  0.221125)
between?    0.590000   0.000000   0.590000 (  0.589178)
cover?      0.580000   0.000000   0.580000 (  0.579952)
include?    0.590000   0.000000   0.590000 (  0.592481)

                user     system      total        real
<           0.240000   0.000000   0.240000 (  0.237692)
and         0.220000   0.000000   0.220000 (  0.220615)
and space   0.220000   0.000000   0.220000 (  0.221509)
&&          0.220000   0.000000   0.220000 (  0.221178)
between?    0.600000   0.000000   0.600000 (  0.596799)
cover?      0.580000   0.000000   0.580000 (  0.579926)
include?    0.590000   0.000000   0.590000 (  0.594068)

                user     system      total        real
<           0.240000   0.000000   0.240000 (  0.237483)
and         0.220000   0.000000   0.220000 (  0.220532)
and space   0.220000   0.000000   0.220000 (  0.221618)
&&          0.230000   0.000000   0.230000 (  0.221055)
between?    0.580000   0.000000   0.580000 (  0.587682)
cover?      0.580000   0.000000   0.580000 (  0.579814)
include?    0.600000   0.000000   0.600000 (  0.593216)

                user     system      total        real
<           0.240000   0.000000   0.240000 (  0.238114)
and         0.210000   0.000000   0.210000 (  0.219626)
and space   0.220000   0.010000   0.230000 (  0.221149)
&&          0.220000   0.000000   0.220000 (  0.221578)
between?    0.590000   0.000000   0.590000 (  0.589574)
cover?      0.580000   0.000000   0.580000 (  0.579924)
include?    0.590000   0.000000   0.590000 (  0.594782)

                user     system      total        real
<           0.240000   0.000000   0.240000 (  0.237504)
and         0.220000   0.000000   0.220000 (  0.220728)
and space   0.220000   0.000000   0.220000 (  0.221041)
&&          0.220000   0.000000   0.220000 (  0.221258)
between?    0.590000   0.000000   0.590000 (  0.589589)
cover?      0.580000   0.000000   0.580000 (  0.579319)
include?    0.590000   0.000000   0.590000 (  0.591118)

一般来说,增加的空间没有什么不同,这是我的预期。轻微差异可能是由于我的机器上有一些工作被解雇了。

答案 4 :(得分:0)

不像3到10之间那么清楚但仍然很小。

20.times do |n|
  id = 40555 if (4..9).include?(n)
end
相关问题