Ruby/Range/Range include
Материал из Wiki.crossplatform.ru
Содержание |
Being a lowercase letter
print "R is within A to Z!" if ("A".."Z").include?("r")
Check the range belongings
c = -2..2 c.all? {|x| x>0} # => false: not all values are > 0 c.any? {|x| x>0} # => true: some values are > 0 c.none? {|x| x>2} # => true: no values are > 2 c.one? {|x| x>0} # => false: more than one value is > 0 c.one? {|x| x>2} # => false: no values are > 2 c.one? {|x| x==2} # => true: one value == 2 [1, 2, 3].all? # => true: no values are nil or false [nil, false].any? # => false: no true values [].none? # => true: no non-false, non-nil values
is an integer in an exclusive range
(1...25) === 25 # => false, out of range if ... used
is an integer in a range
(1..25) === 14 # => true, in range (1..25) === 26 # => false, out of range
Range containability check
puts (1..10) === 5 puts (1..10) === 15 puts (1..10) === 3.14159 puts ("a".."j") === "c" puts ("a".."j") === "z"
Range pattern match
while line = gets puts line if line =~ /start/ .. line =~ /end/ end while gets print if /start/../end/ end
Test if something is included in the set of objects specified by the range.
print "R is within A to Z!" if ("A".."Z").include?("R")