Method: Numeric#step

Defined in:
numeric.c

#step(to = nil, by = 1) {|n| ... } ⇒ self #step(to = nil, by = 1) ⇒ Object #step(to = nil, by: 1) {|n| ... } ⇒ self #step(to = nil, by: 1) ⇒ Object #step(by: 1, to: ) {|n| ... } ⇒ self #step(by: 1, to: ) ⇒ Object #step(by: , to: nil) {|n| ... } ⇒ self #step(by: , to: nil) ⇒ Object

Generates a sequence of numbers; with a block given, traverses the sequence.

Of the Core and Standard Library classes, Integer, Float, and Rational use this implementation.

A quick example:

squares = []
1.step(by: 2, to: 10) {|i| squares.push(i*i) }
squares # => [1, 9, 25, 49, 81]

The generated sequence:

  • Begins with self.

  • Continues at intervals of by (which may not be zero).

  • Ends with the last number that is within or equal to to;

that is, less than or equal to +to+ if +by+ is positive,
greater than or equal to +to+ if +by+ is negative.
If +to+ is +nil+, the sequence is of infinite length.

If a block is given, calls the block with each number in the sequence; returns self. If no block is given, returns an Enumerator::ArithmeticSequence.

Keyword Arguments

With keyword arguments by and to, their values (or defaults) determine the step and limit:

# Both keywords given.
squares = []
4.step(by: 2, to: 10) {|i| squares.push(i*i) }    # => 4
squares # => [16, 36, 64, 100]
cubes = []
3.step(by: -1.5, to: -3) {|i| cubes.push(i*i*i) } # => 3
cubes   # => [27.0, 3.375, 0.0, -3.375, -27.0]
squares = []
1.2.step(by: 0.2, to: 2.0) {|f| squares.push(f*f) }
squares # => [1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0]

squares = []
Rational(6/5).step(by: 0.2, to: 2.0) {|r| squares.push(r*r) }
squares # => [1.0, 1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0]

# Only keyword to given.
squares = []
4.step(to: 10) {|i| squares.push(i*i) }           # => 4
squares # => [16, 25, 36, 49, 64, 81, 100]
# Only by given.

# Only keyword by given
squares = []
4.step(by:2) {|i| squares.push(i*i); break if i > 10 }
squares # => [16, 36, 64, 100, 144]

# No block given.
e = 3.step(by: -1.5, to: -3) # => (3.step(by: -1.5, to: -3))
e.class                      # => Enumerator::ArithmeticSequence

Positional Arguments

With optional positional arguments to and by, their values (or defaults) determine the step and limit:

squares = []
4.step(10, 2) {|i| squares.push(i*i) }    # => 4
squares # => [16, 36, 64, 100]
squares = []
4.step(10) {|i| squares.push(i*i) }
squares # => [16, 25, 36, 49, 64, 81, 100]
squares = []
4.step {|i| squares.push(i*i); break if i > 10 }  # => nil
squares # => [16, 25, 36, 49, 64, 81, 100, 121]

Implementation Notes

If all the arguments are integers, the loop operates using an integer counter.

If any of the arguments are floating point numbers, all are converted to floats, and the loop is executed floor(n + n*Float::EPSILON) + 1 times, where n = (limit - self)/step.

Overloads:

  • #step(to = nil, by = 1) {|n| ... } ⇒ self

    Yields:

    • (n)

    Returns:

    • (self)
  • #step(to = nil, by: 1) {|n| ... } ⇒ self

    Yields:

    • (n)

    Returns:

    • (self)
  • #step(by: 1, to: ) {|n| ... } ⇒ self

    Yields:

    • (n)

    Returns:

    • (self)
  • #step(by: , to: nil) {|n| ... } ⇒ self

    Yields:

    • (n)

    Returns:

    • (self)


3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
# File 'numeric.c', line 3094

static VALUE
num_step(int argc, VALUE *argv, VALUE from)
{
    VALUE to, step;
    int desc, inf;

    if (!rb_block_given_p()) {
        VALUE by = Qundef;

        num_step_extract_args(argc, argv, &to, &step, &by);
        if (!UNDEF_P(by)) {
            step = by;
        }
        if (NIL_P(step)) {
            step = INT2FIX(1);
        }
        else if (rb_equal(step, INT2FIX(0))) {
            rb_raise(rb_eArgError, "step can't be 0");
        }
        if ((NIL_P(to) || rb_obj_is_kind_of(to, rb_cNumeric)) &&
            rb_obj_is_kind_of(step, rb_cNumeric)) {
            return rb_arith_seq_new(from, ID2SYM(rb_frame_this_func()), argc, argv,
                                    num_step_size, from, to, step, FALSE);
        }

        return SIZED_ENUMERATOR_KW(from, 2, ((VALUE [2]){to, step}), num_step_size, FALSE);
    }

    desc = num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
    if (rb_equal(step, INT2FIX(0))) {
        inf = 1;
    }
    else if (RB_FLOAT_TYPE_P(to)) {
        double f = RFLOAT_VALUE(to);
        inf = isinf(f) && (signbit(f) ? desc : !desc);
    }
    else inf = 0;

    if (FIXNUM_P(from) && (inf || FIXNUM_P(to)) && FIXNUM_P(step)) {
        long i = FIX2LONG(from);
        long diff = FIX2LONG(step);

        if (inf) {
            for (;; i += diff)
                rb_yield(LONG2FIX(i));
        }
        else {
            long end = FIX2LONG(to);

            if (desc) {
                for (; i >= end; i += diff)
                    rb_yield(LONG2FIX(i));
            }
            else {
                for (; i <= end; i += diff)
                    rb_yield(LONG2FIX(i));
            }
        }
    }
    else if (!ruby_float_step(from, to, step, FALSE, FALSE)) {
        VALUE i = from;

        if (inf) {
            for (;; i = rb_funcall(i, '+', 1, step))
                rb_yield(i);
        }
        else {
            ID cmp = desc ? '<' : '>';

            for (; !RTEST(rb_funcall(i, cmp, 1, to)); i = rb_funcall(i, '+', 1, step))
                rb_yield(i);
        }
    }
    return from;
}