Ruby-on-rails 如何检查一个数字是否包含在一个范围内(在一个语句中)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6881900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:40:17  来源:igfitidea点击:

How to check if a number is included in a range (in one statement)?

ruby-on-railsrubyruby-on-rails-3numbersrange

提问by Backo

I am using Ruby on Rails 3.0.9 and I would like to check if a number is included in a range. That is, if I have a variable number = 5I would like to check 1 <= number <= 10and retrieve a boolean value if the numbervalue is included in that range.

我正在使用 Ruby on Rails 3.0.9,我想检查一个数字是否包含在一个范围内。也就是说,如果我有一个变量,如果该值包含在该范围内,number = 5我想检查1 <= number <= 10并检索一个布尔值number

I can do that like this:

我可以这样做:

number >= 1 && number <= 10

but I would like to do that in one statement. How can I do that?

但我想在一份声明中做到这一点。我怎样才能做到这一点?

回答by Mario Uher

(1..10).include?(number)is the trick.

(1..10).include?(number)是诀窍。

Btw: If you want to validate a number using ActiveModel::Validations, you can even do:

顺便说一句:如果你想使用 验证一个数字ActiveModel::Validations,你甚至可以这样做:

validates_inclusion_of :number, :in => 1..10

read hereabout validates_inclusion_of

在此处阅读有关 validates_inclusion_of 的信息

or the Rails 3+ way:

或 Rails 3+ 方式:

validates :number, :inclusion => 1..10

回答by tybro0103

Enumerable#include?:

可枚举#include?

(1..10).include? n

Range#cover?:

范围#覆盖?

(1..10).cover? n

Comparable#between?:

可比#之间?

n.between? 1, 10

Numericality Validator:

数值验证器

validates :n, numericality: {only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 10}

Inclusion Validator:

包含验证器

validates :n, inclusion: 1..10

回答by Holin

If it's not part of a validation process you can use #between?:

如果它不是验证过程的一部分,您可以使用#between?

2.between?(1, 4)
=> true

回答by Jon

For accurate error messages on a form submit , try these

要在表单提交上获得准确的错误消息,请尝试这些

validates_numericality_of :tax_rate, greater_than_or_equal_to: 0, less_than_or_equal_to: 1, message: 'must be between 0 & 100'

回答by Sudhir Vishwakarma

Rails 4

导轨 4

if you want it through ActiveModel::Validations you can use

如果你想通过 ActiveModel::Validations 你可以使用

validates_inclusion_of :number, :in => start_number..end_number

or the Rails 3 syntax

或 Rails 3 语法

validates :number, :inclusion => start_number..end_number

But The simplest way i find is

但我发现的最简单的方法是

number.between? start_number, end_number

number.between? start_number, end_number

回答by Michael Kohl

In Ruby 1.9 the most direct translation seems to be Range#cover?:

在 Ruby 1.9 中,最直接的翻译似乎是Range#cover?

Returns true if obj is between beg and end, i.e beg <= obj <= end (or end exclusive when exclude_end? is true).

如果 obj 介于 beg 和 end 之间,即 beg <= obj <= end(或当 exclude_end? 为真时为 end exclusive),则返回 true。

In case you wonder how that's different from Range#include?, it's that the latter iterates over all elements of the range if it's a non-numeric range. See this blog postfor a more detailed explanation.

如果您想知道这与 有Range#include?什么不同,如果它是非数字范围,后者将迭代范围的所有元素。有关更详细的解释,请参阅此博客文章

回答by Lakhani Aliraza

If you want to check particular number exists in custom array,

如果要检查自定义数组中是否存在特定数字,

As for example I want to know whether 5 is included in list=[1,4,6,10] or not

例如,我想知道 list=[1,4,6,10] 中是否包含 5

list.include? 5 => false
list.include? 6 => true