Class: Range

Inherits:
Object show all
Includes:
Enumerable
Defined in:
range.c

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #collect, #detect, #each_with_index, #entries, #find, #find_all, #grep, #inject, #map, #max, #min, #partition, #reject, #select, #sort, #sort_by, #to_a, #zip

Constructor Details

#new(start, end) ⇒ Object

Constructs a range using the given start and end. If the third parameter is omitted or is false, the range will include the end object; otherwise, it will be excluded.



# File 'range.c'

static VALUE
range_initialize(argc, argv, range)
int argc;
VALUE *argv;
VALUE range;
{
VALUE beg, end, flags;

rb_scan_args(argc, argv, "21", &beg, &end, &flags);
/* Ranges are immutable, so that they should be initialized only once. */
if (rb_ivar_defined(range, id_beg)) {
rb_name_error(rb_intern("initialize"), "`initialize' called twice");
}

Instance Method Details

#==(obj) ⇒ Boolean

Returns true only if obj is a Range, has equivalent beginning and end items (by comparing them with ==), and has the same #exclude_end? setting as <i>rng</t>.

(0..2) == (0..2)            #=> true
(0..2) == Range.new(0,2)    #=> true
(0..2) == (0...2)           #=> false

Returns:

  • (Boolean)


# File 'range.c'

static VALUE
range_eq(range, obj)
    VALUE range, obj;
{
    if (range == obj) return Qtrue;
    if (!rb_obj_is_instance_of(obj, rb_obj_class(range)))
    return Qfalse;

    if (!rb_equal(rb_ivar_get(range, id_beg), rb_ivar_get(obj, id_beg)))
    return Qfalse;
    if (!rb_equal(rb_ivar_get(range, id_end), rb_ivar_get(obj, id_end)))
    return Qfalse;

    if (EXCL(range) != EXCL(obj)) return Qfalse;

    return Qtrue;
}

#===(obj) ⇒ Boolean #member?(val) ⇒ Boolean #include?(val) ⇒ Boolean

Returns true if obj is an element of rng, false otherwise. Conveniently, === is the comparison operator used by case statements.

case 79
when 1..50   then   print "low\n"
when 51..75  then   print "medium\n"
when 76..100 then   print "high\n"
end

produces:

high

Overloads:

  • #===(obj) ⇒ Boolean

    Returns:

    • (Boolean)
  • #member?(val) ⇒ Boolean

    Returns:

    • (Boolean)
  • #include?(val) ⇒ Boolean

    Returns:

    • (Boolean)


# File 'range.c'

static VALUE
range_include(range, val)
VALUE range, val;
{
VALUE beg, end;

beg = rb_ivar_get(range, id_beg);
end = rb_ivar_get(range, id_end);
if (r_le(beg, val)) {
if (EXCL(range)) {
    if (r_lt(val, end)) return Qtrue;
}

#firstObject #beginObject

Returns the first object in rng.

Overloads:



# File 'range.c'

static VALUE
range_first(range)
    VALUE range;
{
    return rb_ivar_get(range, id_beg);
}

#each {|i| ... } ⇒ Object

Iterates over the elements rng, passing each in turn to the block. You can only iterate if the start object of the range supports the succ method (which means that you can't iterate over ranges of Float objects).

(10..15).each do |n|
   print n, ' '
end

produces:

10 11 12 13 14 15

Yields:

  • (i)


# File 'range.c'

static VALUE
range_each(range)
VALUE range;
{
VALUE beg, end;

beg = rb_ivar_get(range, id_beg);
end = rb_ivar_get(range, id_end);

if (!rb_respond_to(beg, id_succ)) {
rb_raise(rb_eTypeError, "can't iterate from %s",
     rb_obj_classname(beg));
}

#endObject #lastObject

Returns the object that defines the end of rng.

(1..10).end    #=> 10
(1...10).end   #=> 10

Overloads:



# File 'range.c'

static VALUE
range_last(range)
    VALUE range;
{
    return rb_ivar_get(range, id_end);
}

#eql?(obj) ⇒ Boolean

Returns true only if obj is a Range, has equivalent beginning and end items (by comparing them with #eql?), and has the same #exclude_end? setting as rng.

(0..2) == (0..2)            #=> true
(0..2) == Range.new(0,2)    #=> true
(0..2) == (0...2)           #=> false

Returns:

  • (Boolean)


# File 'range.c'

static VALUE
range_eql(range, obj)
    VALUE range, obj;
{
    if (range == obj) return Qtrue;
    if (!rb_obj_is_instance_of(obj, rb_obj_class(range)))
    return Qfalse;

    if (!rb_eql(rb_ivar_get(range, id_beg), rb_ivar_get(obj, id_beg)))
    return Qfalse;
    if (!rb_eql(rb_ivar_get(range, id_end), rb_ivar_get(obj, id_end)))
    return Qfalse;

    if (EXCL(range) != EXCL(obj)) return Qfalse;

    return Qtrue;
}

#exclude_end?Boolean

Returns true if rng excludes its end value.

Returns:

  • (Boolean)


# File 'range.c'

static VALUE
range_exclude_end_p(range)
    VALUE range;
{
    return EXCL(range) ? Qtrue : Qfalse;
}

#firstObject #beginObject

Returns the first object in rng.

Overloads:



# File 'range.c'

static VALUE
range_first(range)
    VALUE range;
{
    return rb_ivar_get(range, id_beg);
}

#hashFixnum

Generate a hash value such that two ranges with the same start and end points, and the same value for the "exclude end" flag, generate the same hash value.

Returns:



# File 'range.c'

static VALUE
range_hash(range)
    VALUE range;
{
    long hash = EXCL(range);
    VALUE v;

    v = rb_hash(rb_ivar_get(range, id_beg));
    hash ^= v << 1;
    v = rb_hash(rb_ivar_get(range, id_end));
    hash ^= v << 9;
    hash ^= EXCL(range) << 24;

    return LONG2FIX(hash);
}

#===(obj) ⇒ Boolean #member?(val) ⇒ Boolean #include?(val) ⇒ Boolean

Returns true if obj is an element of rng, false otherwise. Conveniently, === is the comparison operator used by case statements.

case 79
when 1..50   then   print "low\n"
when 51..75  then   print "medium\n"
when 76..100 then   print "high\n"
end

produces:

high

Overloads:

  • #===(obj) ⇒ Boolean

    Returns:

    • (Boolean)
  • #member?(val) ⇒ Boolean

    Returns:

    • (Boolean)
  • #include?(val) ⇒ Boolean

    Returns:

    • (Boolean)


# File 'range.c'

static VALUE
range_include(range, val)
VALUE range, val;
{
VALUE beg, end;

beg = rb_ivar_get(range, id_beg);
end = rb_ivar_get(range, id_end);
if (r_le(beg, val)) {
if (EXCL(range)) {
    if (r_lt(val, end)) return Qtrue;
}

#inspectString

Convert this range object to a printable form (using inspect to convert the start and end objects).

Returns:



# File 'range.c'

static VALUE
range_inspect(range)
    VALUE range;
{
    VALUE str, str2;

    str = rb_inspect(rb_ivar_get(range, id_beg));
    str2 = rb_inspect(rb_ivar_get(range, id_end));
    str = rb_str_dup(str);
    rb_str_cat(str, "...", EXCL(range)?3:2);
    rb_str_append(str, str2);
    OBJ_INFECT(str, str2);

    return str;
}

#endObject #lastObject

Returns the object that defines the end of rng.

(1..10).end    #=> 10
(1...10).end   #=> 10

Overloads:



# File 'range.c'

static VALUE
range_last(range)
    VALUE range;
{
    return rb_ivar_get(range, id_end);
}

#===(obj) ⇒ Boolean #member?(val) ⇒ Boolean #include?(val) ⇒ Boolean

Returns true if obj is an element of rng, false otherwise. Conveniently, === is the comparison operator used by case statements.

case 79
when 1..50   then   print "low\n"
when 51..75  then   print "medium\n"
when 76..100 then   print "high\n"
end

produces:

high

Overloads:

  • #===(obj) ⇒ Boolean

    Returns:

    • (Boolean)
  • #member?(val) ⇒ Boolean

    Returns:

    • (Boolean)
  • #include?(val) ⇒ Boolean

    Returns:

    • (Boolean)


# File 'range.c'

static VALUE
range_include(range, val)
VALUE range, val;
{
VALUE beg, end;

beg = rb_ivar_get(range, id_beg);
end = rb_ivar_get(range, id_end);
if (r_le(beg, val)) {
if (EXCL(range)) {
    if (r_lt(val, end)) return Qtrue;
}

#step(n = 1) {|obj| ... } ⇒ Object

Iterates over rng, passing each nth element to the block. If the range contains numbers or strings, natural ordering is used. Otherwise step invokes succ to iterate through range elements. The following code uses class Xs, which is defined in the class-level documentation.

range = Xs.new(1)..Xs.new(10)
range.step(2) {|x| puts x}
range.step(3) {|x| puts x}

produces:

 1 x
 3 xxx
 5 xxxxx
 7 xxxxxxx
 9 xxxxxxxxx
 1 x
 4 xxxx
 7 xxxxxxx
10 xxxxxxxxxx

Yields:

  • (obj)


# File 'range.c'

static VALUE
range_step(argc, argv, range)
int argc;
VALUE *argv;
VALUE range;
{
VALUE b, e, step;
long unit;

b = rb_ivar_get(range, id_beg);
e = rb_ivar_get(range, id_end);
if (rb_scan_args(argc, argv, "01", &step) == 0) {
step = INT2FIX(1);
}

#to_sString

Convert this range object to a printable form.

Returns:



# File 'range.c'

static VALUE
range_to_s(range)
    VALUE range;
{
    VALUE str, str2;

    str = rb_obj_as_string(rb_ivar_get(range, id_beg));
    str2 = rb_obj_as_string(rb_ivar_get(range, id_end));
    str = rb_str_dup(str);
    rb_str_cat(str, "...", EXCL(range)?3:2);
    rb_str_append(str, str2);
    OBJ_INFECT(str, str2);

    return str;
}