有没有办法在 Ruby 中测试 3
发布于 2024-12-21 07:40:54 字数 120 浏览 0 评论 0 原文

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

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

In ruby is there a way to do the following

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

も让我眼熟你 2024-12-28 07:40:54

您可以使用两种比较:

if 3 < n && n < 10

或者您可以使用 Between

if n.between? 3, 10

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

if (3..10).cover? n

请注意,最后两个包括端点。不过,范围可以选择排除其端点。

顺便说一句,在代码审查中,我会将像 3 和 10 这样的“神奇数字”标记为需要重构为适当命名的自记录方法,例如 if inside_tolerance?(n) 或类似的。

You can use two comparisons:

if 3 < n && n < 10

or you can use between:

if n.between? 3, 10

or you can use a range and the cover? (1.9) or include? (1.8) methods:

if (3..10).cover? n

Note though that the last two include the endpoints. A range can optionally exclude its endpoint though.

BTW, in a code review I'd flag "magic numbers" like the 3 and 10 as something that needs to be refactored into an appropriately named, self-documenting method, something like if within_tolerance?(n) or similar.

青柠芒果 2024-12-28 07:40:54
n.between?(3, 10)

(感谢谦虚的 Comparable 模块)

n.between?(3, 10)

(Thanks to the humble Comparable module)

计㈡愣 2024-12-28 07:40:54
20.times do |n|
  if 3 < n && n < 10
    id = 40555
  end
end
20.times do |n|
  if 3 < n && n < 10
    id = 40555
  end
end
一场信仰旅途 2024-12-28 07:40:54

探究各种实现的速度始终是一个很好的练习。

为了欢乐,我将评论中的建议代码添加到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)

一般来说,添加的空格不会产生任何影响,这正是我所期望的。微小的差异可能是由于我的机器上的一些工作被解雇了。

It's always a good exercise to poke at the speed of various implementations.

For jollies I added the suggested code from the comment to the OP's question, which surprised me. I wouldn't have expected the change in the test to improve the speed, but it does make a minor difference. To understand why I added spaces, in case it was a line parsing issue, which I didn't think it would be. And, to rule out it was the and vs. && operators I added those.

#!/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)

In general the added spaces don't make a difference which is what I expected. The minor variance is probably due to some job firing on my machine.

汹涌人海 2024-12-28 07:40:54

不像 3 到 10 那样清晰,但仍然很小。

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

Not as clear as between 3 and 10 but still minimal.

20.times do |n|
  id = 40555 if (4..9).include?(n)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文