D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Range
/
Filename :
cdesc-Range.ri
back
Copy
U:RDoc::NormalClass[iI" Range:ET@I"Object;To:RDoc::Markup::Document:@parts[o;;[ : @fileI"#ext/json/lib/json/add/range.rb;T:0@omit_headings_from_table_of_contents_below0o;;[Xo:RDoc::Markup::Paragraph;[I"7A \Range object represents a collection of values ;TI"1that are between given begin and end values.;To:RDoc::Markup::BlankLine o;;[I"5You can create an \Range object explicitly with:;T@o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0;[o;;[I"EA {range literal}[rdoc-ref:syntax/literals.rdoc@Range+Literals]:;T@o:RDoc::Markup::Verbatim;[I"<# Ranges that use '..' to include the given end value. ;TI"((1..4).to_a # => [1, 2, 3, 4] ;TI"0('a'..'d').to_a # => ["a", "b", "c", "d"] ;TI"=# Ranges that use '...' to exclude the given end value. ;TI"%(1...4).to_a # => [1, 2, 3] ;TI"+('a'...'d').to_a # => ["a", "b", "c"] ;T:@format0o;;0;[ o;;[I"Method Range.new:;T@o;;[I";# Ranges that by default include the given end value. ;TI"0Range.new(1, 4).to_a # => [1, 2, 3, 4] ;TI"8Range.new('a', 'd').to_a # => ["a", "b", "c", "d"] ;TI"T# Ranges that use third argument +exclude_end+ to exclude the given end value. ;TI"3Range.new(1, 4, true).to_a # => [1, 2, 3] ;TI"8Range.new('a', 'd', true).to_a # => ["a", "b", "c"];T@S:RDoc::Markup::Heading: leveli: textI"Beginless Ranges;T@o;;[I"NA _beginless_ _range_ has a definite end value, but a +nil+ begin value. ;TI":Such a range includes all values up to the end value.;T@o;;[I")r = (..4) # => nil..4 ;TI"&r.begin # => nil ;TI"'r.include?(-50) # => true ;TI"'r.include?(4) # => true ;TI" ;TI"*r = (...4) # => nil...4 ;TI"(r.include?(4) # => false ;TI" ;TI")Range.new(nil, 4) # => nil..4 ;TI"*Range.new(nil, 4, true) # => nil...4 ;T;0o;;[I"5A beginless range may be used to slice an array:;T@o;;[I"a = [1, 2, 3, 4] ;TI"4# Include the third array element in the slice ;TI"r = (..2) # => nil..2 ;TI"a[r] # => [1, 2, 3] ;TI"6# Exclude the third array element from the slice ;TI"r = (...2) # => nil...2 ;TI"a[r] # => [1, 2] ;T;0o;;[I">\Method +each+ for a beginless range raises an exception.;T@S;;i;I"Endless Ranges;T@o;;[I"MAn _endless_ _range_ has a definite begin value, but a +nil+ end value. ;TI";Such a range includes all values from the begin value.;T@o;;[ I" r = (1..) # => 1.. ;TI" r.end # => nil ;TI"!r.include?(50) # => true ;TI" ;TI" Range.new(1, nil) # => 1.. ;T;0o;;[ I"JThe literal for an endless range may be written with either two dots ;TI"or three. ;TI"2The range has the same elements, either way. ;TI")But note that the two are not equal:;T@o;;[ I"#r0 = (1..) # => 1.. ;TI"$r1 = (1...) # => 1... ;TI"$r0.begin == r1.begin # => true ;TI"$r0.end == r1.end # => true ;TI"%r0 == r1 # => false ;T;0o;;[I"4An endless range may be used to slice an array:;T@o;;[I"a = [1, 2, 3, 4] ;TI"r = (2..) # => 2.. ;TI"a[r] # => [3, 4] ;T;0o;;[I"L\Method +each+ for an endless range calls the given block indefinitely:;T@o;;[I"a = [] ;TI"r = (1..) ;TI"r.each do |i| ;TI" a.push(i) if i.even? ;TI" break if i > 10 ;TI" end ;TI"a # => [2, 4, 6, 8, 10] ;T;0o;;[ I"PA range can be both beginless and endless. For literal beginless, endless ;TI"Lranges, at least the beginning or end of the range must be given as an ;TI"Pexplicit nil value. It is recommended to use an explicit nil beginning and ;TI"Fimplicit nil end, since that is what Ruby uses for Range#inspect:;T@o;;[I"(nil..) # => (nil..) ;TI"(..nil) # => (nil..) ;TI"(nil..nil) # => (nil..) ;T;0S;;i;I"Ranges and Other Classes;T@o;;[ I"?An object may be put into a range if its class implements ;TI"$instance method <tt>#<=></tt>. ;TI"FRuby core classes that do so include Array, Complex, File::Stat, ;TI"QFloat, Integer, Kernel, Module, Numeric, Rational, String, Symbol, and Time.;T@o;;[I" Example:;T@o;;[ I"Bt0 = Time.now # => 2021-09-19 09:22:48.4854986 -0500 ;TI"Bt1 = Time.now # => 2021-09-19 09:22:56.0365079 -0500 ;TI"Bt2 = Time.now # => 2021-09-19 09:23:08.5263283 -0500 ;TI"%(t0..t2).include?(t1) # => true ;TI"&(t0..t1).include?(t2) # => false ;T;0o;;[ I"7A range can be iterated over only if its elements ;TI"'implement instance method +succ+. ;TI"FRuby core classes that do so include Integer, String, and Symbol ;TI"1(but not the other classes mentioned above).;T@o;;[I"Iterator methods include:;T@o; ;;;[o;;0;[o;;[I"+In \Range itself: #each, #step, and #%;To;;0;[o;;[I"EIncluded from module Enumerable: #each_entry, #each_with_index, ;TI"C#each_with_object, #each_slice, #each_cons, and #reverse_each.;T@o;;[I" Example:;T@o;;[I"a = [] ;TI""(1..4).each {|i| a.push(i) } ;TI"a # => [1, 2, 3, 4] ;T;0S;;i;I"$Ranges and User-Defined Classes;T@o;;[ I"8A user-defined class that is to be used in a range ;TI"3must implement instance method <tt>#<=></tt>; ;TI"see Integer#<=>. ;TI"9To make iteration available, it must also implement ;TI".instance method +succ+; see Integer#succ.;T@o;;[ I"?The class below implements both <tt>#<=></tt> and +succ+, ;TI"Kand so can be used both to construct ranges and to iterate over them. ;TI"1Note that the Comparable module is included ;TI"Dso the <tt>==</tt> method is defined in terms of <tt>#<=></tt>.;T@o;;[I"-# Represent a string of 'X' characters. ;TI"class Xs ;TI" include Comparable ;TI" attr_accessor :length ;TI" def initialize(n) ;TI" @length = n ;TI" end ;TI" def succ ;TI" Xs.new(@length + 1) ;TI" end ;TI" def <=>(other) ;TI"" @length <=> other.length ;TI" end ;TI" def to_s ;TI"+ sprintf "%2d #{inspect}", @length ;TI" end ;TI" def inspect ;TI" 'X' * @length ;TI" end ;TI" end ;TI" ;TI".r = Xs.new(3)..Xs.new(6) #=> XXX..XXXXXX ;TI"=r.to_a #=> [XXX, XXXX, XXXXX, XXXXXX] ;TI"'r.include?(Xs.new(5)) #=> true ;TI"(r.include?(Xs.new(7)) #=> false ;T;0S;;i;I"What's Here;T@o;;[I",First, what's elsewhere. \Class \Range:;T@o; ;;;[o;;0;[o;;[I"AInherits from {class Object}[rdoc-ref:Object@What-27s+Here].;To;;0;[o;;[I"FIncludes {module Enumerable}[rdoc-ref:Enumerable@What-27s+Here], ;TI"1which provides dozens of additional methods.;T@o;;[I"=Here, class \Range provides methods that are useful for:;T@o; ;;;[o;;0;[o;;[I"D{Creating a Range}[rdoc-ref:Range@Methods+for+Creating+a+Range];To;;0;[o;;[I"4{Querying}[rdoc-ref:Range@Methods+for+Querying];To;;0;[o;;[I"6{Comparing}[rdoc-ref:Range@Methods+for+Comparing];To;;0;[o;;[I"6{Iterating}[rdoc-ref:Range@Methods+for+Iterating];To;;0;[o;;[I"8{Converting}[rdoc-ref:Range@Methods+for+Converting];To;;0;[o;;[I"R{Methods for Working with JSON}[rdoc-ref:Range@Methods+for+Working+with+JSON];T@S;;i;I""Methods for Creating a \Range;T@o; ;;;[o;;0;[o;;[I" ::new: Returns a new range.;T@S;;i;I"Methods for Querying;T@o; ;;;[o;;0;[o;;[I"6#begin: Returns the begin value given for +self+.;To;;0;[o;;[I"J#bsearch: Returns an element from +self+ selected by a binary search.;To;;0;[o;;[I"3#count: Returns a count of elements in +self+.;To;;0;[o;;[I"2#end: Returns the end value given for +self+.;To;;0;[o;;[I"?#exclude_end?: Returns whether the end object is excluded.;To;;0;[o;;[I"2#first: Returns the first elements of +self+.;To;;0;[o;;[I"*#hash: Returns the integer hash code.;To;;0;[o;;[I"0#last: Returns the last elements of +self+.;To;;0;[o;;[I"0#max: Returns the maximum values in +self+.;To;;0;[o;;[I"0#min: Returns the minimum values in +self+.;To;;0;[o;;[I"?#minmax: Returns the minimum and maximum values in +self+.;To;;0;[o;;[I"4#size: Returns the count of elements in +self+.;T@S;;i;I"Methods for Comparing;T@o; ;;;[ o;;0;[o;;[I"G#==: Returns whether a given object is equal to +self+ (uses #==).;To;;0;[o;;[I"P#===: Returns whether the given object is between the begin and end values.;To;;0;[o;;[I">#cover?: Returns whether a given object is within +self+.;To;;0;[o;;[I"K#eql?: Returns whether a given object is equal to +self+ (uses #eql?).;To;;0;[o;;[I"E#include? (aliased as #member?): Returns whether a given object ;TI"is an element of +self+.;T@S;;i;I"Methods for Iterating;T@o; ;;;[o;;0;[o;;[I"S#%: Requires argument +n+; calls the block with each +n+-th element of +self+.;To;;0;[o;;[I"8#each: Calls the block with each element of +self+.;To;;0;[o;;[I"9#step: Takes optional argument +n+ (defaults to 1); ;TI"8calls the block with each +n+-th element of +self+.;T@S;;i;I"Methods for Converting;T@o; ;;;[o;;0;[o;;[I"I#inspect: Returns a string representation of +self+ (uses #inspect).;To;;0;[o;;[I"I#to_a (aliased as #entries): Returns elements of +self+ in an array.;To;;0;[o;;[I"C#to_s: Returns a string representation of +self+ (uses #to_s).;T@S;;i;I"#Methods for Working with \JSON;T@o; ;;;[o;;0;[o;;[I"R::json_create: Returns a new \Range object constructed from the given object.;To;;0;[o;;[I"<#as_json: Returns a 2-element hash representing +self+.;To;;0;[o;;[I":#to_json: Returns a \JSON string representing +self+.;T@o;;[I"%To make these methods available:;T@o;;[I"require 'json/add/range';T;0; I"range.c;T; 0; 0; 0[ [ [[I"Enumerable;To;;[ ; @�; 0I"range.c;T[[I" class;T[[:public[[I"json_create;TI"#ext/json/lib/json/add/range.rb;T[I"new;T@�[:protected[ [:private[ [I" instance;T[[;["[I"%;T@�[I"==;T@�[I"===;T@�[I"as_json;T@�[I" begin;T@�[I"bsearch;T@�[I" count;T@�[I"cover?;T@�[I" each;T@�[I"end;T@�[I"entries;T@�[I" eql?;T@�[I"exclude_end?;T@�[I" first;T@�[I" hash;T@�[I" include?;T@�[I"inspect;T@�[I" last;T@�[I"max;T@�[I"member?;T@�[I"min;T@�[I"minmax;T@�[I" overlap?;T@�[I"reverse_each;T@�[I" size;T@�[I" step;T@�[I" to_a;T@�[I"to_json;T@�[I" to_s;T@�[;[ [;[ [ [U:RDoc::Context::Section[i 0o;;[ ; 0; 0[@ I"lib/pp.rb;T@�@�cRDoc::TopLevel