D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Numeric
/
Filename :
step-i.ri
back
Copy
U:RDoc::AnyMethod[iI" step:ETI"Numeric#step;TF:publico:RDoc::Markup::Document:@parts[ o:RDoc::Markup::Paragraph; [I"QGenerates a sequence of numbers; with a block given, traverses the sequence.;To:RDoc::Markup::BlankLine o; ; [I"/Of the Core and Standard Library classes, ;TI":Integer, Float, and Rational use this implementation.;T@o; ; [I"A quick example:;T@o:RDoc::Markup::Verbatim; [I"squares = [] ;TI"41.step(by: 2, to: 10) {|i| squares.push(i*i) } ;TI"%squares # => [1, 9, 25, 49, 81] ;T:@format0o; ; [I"The generated sequence:;T@o:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0; [o; ; [I"Begins with +self+.;To;;0; [o; ; [I"<Continues at intervals of +by+ (which may not be zero).;To;;0; [o; ; [ I"@Ends with the last number that is within or equal to +to+; ;TI">that is, less than or equal to +to+ if +by+ is positive, ;TI"8greater than or equal to +to+ if +by+ is negative. ;TI":If +to+ is +nil+, the sequence is of infinite length.;T@o; ; [I"LIf a block is given, calls the block with each number in the sequence; ;TI"Ureturns +self+. If no block is given, returns an Enumerator::ArithmeticSequence.;T@o; ; [I"<b>Keyword Arguments</b>;T@o; ; [I"+With keyword arguments +by+ and +to+, ;TI"=their values (or defaults) determine the step and limit:;T@o;; ["I"# Both keywords given. ;TI"squares = [] ;TI">4.step(by: 2, to: 10) {|i| squares.push(i*i) } # => 4 ;TI"$squares # => [16, 36, 64, 100] ;TI"cubes = [] ;TI">3.step(by: -1.5, to: -3) {|i| cubes.push(i*i*i) } # => 3 ;TI"4cubes # => [27.0, 3.375, 0.0, -3.375, -27.0] ;TI"squares = [] ;TI"91.2.step(by: 0.2, to: 2.0) {|f| squares.push(f*f) } ;TI"Lsquares # => [1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0] ;TI" ;TI"squares = [] ;TI"CRational(6/5).step(by: 0.2, to: 2.0) {|r| squares.push(r*r) } ;TI"Qsquares # => [1.0, 1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0] ;TI" ;TI"# Only keyword to given. ;TI"squares = [] ;TI">4.step(to: 10) {|i| squares.push(i*i) } # => 4 ;TI"0squares # => [16, 25, 36, 49, 64, 81, 100] ;TI"# Only by given. ;TI" ;TI"# Only keyword by given ;TI"squares = [] ;TI"<4.step(by:2) {|i| squares.push(i*i); break if i > 10 } ;TI")squares # => [16, 36, 64, 100, 144] ;TI" ;TI"# No block given. ;TI"Be = 3.step(by: -1.5, to: -3) # => (3.step(by: -1.5, to: -3)) ;TI"Fe.class # => Enumerator::ArithmeticSequence ;T; 0o; ; [I" <b>Positional Arguments</b>;T@o; ; [I"7With optional positional arguments +to+ and +by+, ;TI"=their values (or defaults) determine the step and limit:;T@o;; [I"squares = [] ;TI"64.step(10, 2) {|i| squares.push(i*i) } # => 4 ;TI"$squares # => [16, 36, 64, 100] ;TI"squares = [] ;TI")4.step(10) {|i| squares.push(i*i) } ;TI"0squares # => [16, 25, 36, 49, 64, 81, 100] ;TI"squares = [] ;TI"@4.step {|i| squares.push(i*i); break if i > 10 } # => nil ;TI"5squares # => [16, 25, 36, 49, 64, 81, 100, 121] ;T; 0o; ; [I" <b>Implementation Notes</b>;T@o; ; [I"KIf all the arguments are integers, the loop operates using an integer ;TI" counter.;T@o; ; [ I"KIf any of the arguments are floating point numbers, all are converted ;TI")to floats, and the loop is executed ;TI"3<i>floor(n + n*Float::EPSILON) + 1</i> times, ;TI"*where <i>n = (limit - self)/step</i>.;T: @fileI"numeric.c;T:0@omit_headings_from_table_of_contents_below0I"pstep(to = nil, by = 1) {|n| ... } -> self step(to = nil, by = 1) -> enumerator step(to = nil, by: 1) {|n| ... } -> self step(to = nil, by: 1) -> enumerator step(by: 1, to: ) {|n| ... } -> self step(by: 1, to: ) -> enumerator step(by: , to: nil) {|n| ... } -> self step(by: , to: nil) -> enumerator ;T0[ I"(*args);T@{FI"Numeric;TcRDoc::NormalClass00