Class: Range

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
mrblib/range.rb,
mrblib/range.rb,
src/range.c

Overview

15.2.14

Instance Method Summary (collapse)

Methods included from Enumerable

#__sort_sub__, #all?, #any?, #collect, #detect, #each_with_index, #entries, #find_all, #grep, #include?, #inject, #max, #min, #partition, #reject, #sort

Constructor Details

- (Object) new(start, end)

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.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'src/range.c', line 120

mrb_value
mrb_range_initialize(mrb_state *mrb, mrb_value range)
{
  mrb_value beg, end;
  mrb_bool exclusive;
  int n;

  n = mrb_get_args(mrb, "oo|b", &beg, &end, &exclusive);
  if (n != 3) {
    exclusive = FALSE;
  }
  /* Ranges are immutable, so that they should be initialized only once. */
  range_init(mrb, range, beg, end, exclusive);
  return range;
}

Instance Method Details

- (Object) first - (Object) begin

Returns the first object in rng.

Overloads:

  • - (Object) first

    Returns:

    • (Object)
  • - (Object) begin

    Returns:

    • (Object)


57
58
59
60
61
62
63
# File 'src/range.c', line 57

mrb_value
mrb_range_beg(mrb_state *mrb, mrb_value range)
{
  struct RRange *r = mrb_range_ptr(range);

  return r->edges->beg;
}

- (Object) each(&block)

Calls the given block for each element of self and pass the respective element.

ISO 15.2.14.4.4

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'mrblib/range.rb', line 12

def each(&block)
  return to_enum :each unless block_given?

  val = self.first
  last = self.last

  if val.kind_of?(Fixnum) && last.kind_of?(Fixnum) # fixnums are special
    lim = last
    lim += 1 unless exclude_end?
    i = val
    while i < lim
      block.call(i)
      i += 1
    end
    return self
  end

  raise TypeError, "can't iterate" unless val.respond_to? :succ

  return self if (val <=> last) > 0

  while (val <=> last) < 0
    block.call(val)
    val = val.succ
  end

  block.call(val) if !exclude_end? && (val <=> last) == 0
  self
end

- (Object) end - (Object) last

Returns the object that defines the end of rng.

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

Overloads:

  • - (Object) end

    Returns:

    • (Object)
  • - (Object) last

    Returns:

    • (Object)


76
77
78
79
80
81
82
# File 'src/range.c', line 76

mrb_value
mrb_range_end(mrb_state *mrb, mrb_value range)
{
  struct RRange *r = mrb_range_ptr(range);

  return r->edges->end;
}

- (Object) first - (Object) begin

Returns the first object in rng.

Overloads:

  • - (Object) first

    Returns:

    • (Object)
  • - (Object) begin

    Returns:

    • (Object)


57
58
59
60
61
62
63
# File 'src/range.c', line 57

mrb_value
mrb_range_beg(mrb_state *mrb, mrb_value range)
{
  struct RRange *r = mrb_range_ptr(range);

  return r->edges->beg;
}

- (Object) hash

redefine #hash 15.3.1.3.15



43
44
45
46
47
# File 'mrblib/range.rb', line 43

def hash
  h = first.hash ^ last.hash
  h += 1 if self.exclude_end?
  h
end

- (Object) initialize_copy

15.2.14.4.15(x)



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'src/range.c', line 363

static mrb_value
range_initialize_copy(mrb_state *mrb, mrb_value copy)
{
  mrb_value src;
  struct RRange *r;

  mrb_get_args(mrb, "o", &src);

  if (mrb_obj_equal(mrb, copy, src)) return copy;
  if (!mrb_obj_is_instance_of(mrb, src, mrb_obj_class(mrb, copy))) {
    mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class");
  }

  r = mrb_range_ptr(src);
  range_init(mrb, copy, r->edges->beg, r->edges->end, r->excl);

  return copy;
}

- (String) inspect

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

Returns:



308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'src/range.c', line 308

static mrb_value
range_inspect(mrb_state *mrb, mrb_value range)
{
  mrb_value str, str2;
  struct RRange *r = mrb_range_ptr(range);

  str  = mrb_inspect(mrb, r->edges->beg);
  str2 = mrb_inspect(mrb, r->edges->end);
  str  = mrb_str_dup(mrb, str);
  mrb_str_cat(mrb, str, "...", r->excl ? 3 : 2);
  mrb_str_cat_str(mrb, str, str2);

  return str;
}

- (Object) end - (Object) last

Returns the object that defines the end of rng.

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

Overloads:

  • - (Object) end

    Returns:

    • (Object)
  • - (Object) last

    Returns:

    • (Object)


76
77
78
79
80
81
82
# File 'src/range.c', line 76

mrb_value
mrb_range_end(mrb_state *mrb, mrb_value range)
{
  struct RRange *r = mrb_range_ptr(range);

  return r->edges->end;
}

- (String) to_s

Convert this range object to a printable form.

Returns:



283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'src/range.c', line 283

static mrb_value
range_to_s(mrb_state *mrb, mrb_value range)
{
  mrb_value str, str2;
  struct RRange *r = mrb_range_ptr(range);

  str  = mrb_obj_as_string(mrb, r->edges->beg);
  str2 = mrb_obj_as_string(mrb, r->edges->end);
  str  = mrb_str_dup(mrb, str);
  mrb_str_cat(mrb, str, "...", r->excl ? 3 : 2);
  mrb_str_cat_str(mrb, str, str2);

  return str;
}