Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

documents the backported Range#cover?, and related stuff

  • Loading branch information...
commit e2d54fb3f17ac30c91d855c45060ec5518b75ec5 1 parent e984fd4
@fxn fxn authored
View
8 activemodel/lib/active_model/validations/inclusion.rb
@@ -14,9 +14,9 @@ def validate_each(record, attribute, value)
record.errors.add(attribute, :inclusion, options.except(:in).merge!(:value => value)) unless options[:in].send(include?, value)
end
- # On Ruby 1.9 Range#include? checks all possible values in the range for equality,
- # so it may be slow for large ranges. The new Range#cover? uses the previous logic
- # of comparing a value with the range endpoints.
+ # In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the
+ # range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt>
+ # uses the previous logic of comparing a value with the range endpoints.
def include?
options[:in].is_a?(Range) ? :cover? : :include?
end
@@ -33,6 +33,8 @@ module HelperMethods
#
# Configuration options:
# * <tt>:in</tt> - An enumerable object of available items.
+ # If the enumerable is a range the test is performed with <tt>Range#cover?</tt>
+ # (backported in Active Support for 1.8), otherwise with <tt>include?</tt>.
# * <tt>:message</tt> - Specifies a custom error message (default is: "is not included in the list").
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
View
10 railties/guides/source/active_support_core_extensions.textile
@@ -528,7 +528,7 @@ The default value can be also specified with a block, which is called in the con
class User
attr_accessor :name, :surname
attr_accessor_with_default(:full_name) {
- [name, surname].compact.join(" ")
+ [name, surname].compact.join(" ")
}
end
@@ -2713,6 +2713,14 @@ WARNING: The original +Range#include?+ is still the one aliased to +Range#===+.
NOTE: Defined in +active_support/core_ext/range/include_range.rb+.
+h4. +cover?+
+
+Ruby 1.9 provides +cover?+, and Active Support defines it for previous versions as an alias for +include?+.
+
+The method +include?+ in Ruby 1.9 is different from the one in 1.8 for non-numeric ranges: instead of being based on comparisons between the value and the range's endpoints, it walks the range with +succ+ looking for value. This works better for ranges with holes, but it has different complexity and may not finish in some other cases.
+
+In Ruby 1.9 the old behavior is still available in the new +cover?+, which Active Support backports for forward compatibility. For example, Rails uses +cover?+ for ranges in +validates_inclusion_of+.
+
h4. +overlaps?+
The method +Range#overlaps?+ says whether any two given ranges have non-void intersection:
Please sign in to comment.
Something went wrong with that request. Please try again.