D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Range
/
Filename :
step-i.ri
back
Copy
U:RDoc::AnyMethod[iI" step:ETI"Range#step;TF:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"UIterates over the elements of range in steps of +s+. The iteration is performed ;TI"by <tt>+</tt> operator:;To:RDoc::Markup::BlankLine o:RDoc::Markup::Verbatim; [I")(0..6).step(2) { puts _1 } #=> 1..5 ;TI"# Prints: 0, 2, 4, 6 ;TI" ;TI"=# Iterate between two dates in step of 1 day (24 hours) ;TI"N(Time.utc(2022, 2, 24)..Time.utc(2022, 3, 1)).step(24*60*60) { puts _1 } ;TI"# Prints: ;TI"!# 2022-02-24 00:00:00 UTC ;TI"!# 2022-02-25 00:00:00 UTC ;TI"!# 2022-02-26 00:00:00 UTC ;TI"!# 2022-02-27 00:00:00 UTC ;TI"!# 2022-02-28 00:00:00 UTC ;TI"!# 2022-03-01 00:00:00 UTC ;T:@format0o; ; [I"PIf <tt> + step</tt> decreases the value, iteration is still performed when ;TI"+step +begin+ is higher than the +end+:;T@o;; [I"!(0..6).step(-2) { puts _1 } ;TI"# Prints nothing ;TI" ;TI"!(6..0).step(-2) { puts _1 } ;TI"# Prints: 6, 4, 2, 0 ;TI" ;TI"O(Time.utc(2022, 3, 1)..Time.utc(2022, 2, 24)).step(-24*60*60) { puts _1 } ;TI"# Prints: ;TI"!# 2022-03-01 00:00:00 UTC ;TI"!# 2022-02-28 00:00:00 UTC ;TI"!# 2022-02-27 00:00:00 UTC ;TI"!# 2022-02-26 00:00:00 UTC ;TI"!# 2022-02-25 00:00:00 UTC ;TI"!# 2022-02-24 00:00:00 UTC ;T; 0o; ; [I"PWhen the block is not provided, and range boundaries and step are Numeric, ;TI"7the method returns Enumerator::ArithmeticSequence.;T@o;; [I"*(1..5).step(2) # => ((1..5).step(2)) ;TI"/(1.0..).step(1.5) #=> ((1.0..).step(1.5)) ;TI"1(..3r).step(1/3r) #=> ((..3/1).step((1/3))) ;T; 0o; ; [I"XEnumerator::ArithmeticSequence can be further used as a value object for iteration ;TI"Uor slicing of collections (see Array#[]). There is a convenience method #% with ;TI"Rbehavior similar to +step+ to produce arithmetic sequences more expressively:;T@o;; [I"# Same as (1..5).step(2) ;TI"#(1..5) % 2 # => ((1..5).%(2)) ;T; 0o; ; [I"OIn a generic case, when the block is not provided, Enumerator is returned:;T@o;; [I"B('a'..).step('b') #=> #<Enumerator: "a"..:step("b")> ;TI"6('a'..).step('b').take(3) #=> ["a", "ab", "abb"] ;T; 0o; ; [I"RIf +s+ is not provided, it is considered +1+ for ranges with numeric +begin+:;T@o;; [I"(1..5).step { p _1 } ;TI"# Prints: 1, 2, 3, 4, 5 ;T; 0o; ; [I"6For non-Numeric ranges, step absence is an error:;T@o;; [I"A(Time.utc(2022, 3, 1)..Time.utc(2022, 2, 24)).step { p _1 } ;TI"G# raises: step is required for non-numeric ranges (ArgumentError) ;T; 0o; ; [I"WFor backward compatibility reasons, String ranges support the iteration both with ;TI"Wstring step and with integer step. In the latter case, the iteration is performed ;TI"5by calculating the next values with String#succ:;T@o;; [ I"!('a'..'e').step(2) { p _1 } ;TI"# Prints: a, c, e ;TI"('a'..'e').step { p _1 } ;TI",# Default step 1; prints: a, b, c, d, e;T; 0: @fileI"range.c;T:0@omit_headings_from_table_of_contents_below0I"istep(s = 1) {|element| ... } -> self step(s = 1) -> enumerator/arithmetic_sequence ;T0[ I"(*args);T@dFI" Range;TcRDoc::NormalClass00