Module: Kernel

Defined in:
mrblib/kernel.rb,
src/kernel.c,
mrbgems/mruby-sprintf/src/kernel.c,
mrbgems/mruby-io/mrblib/kernel.rb,
mrbgems/mruby-print/mrblib/print.rb,
mrbgems/mruby-method/mrblib/kernel.rb,
mrbgems/mruby-object-ext/mrblib/object.rb,
mrbgems/mruby-rational/mrblib/rational.rb,
mrbgems/mruby-enumerator/mrblib/enumerator.rb

Overview

Kernel

ISO 15.3.1

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject

15.3.1.3.30



689
690
691
692
693
694
695
696
697
698
699
700
# File 'src/kernel.c', line 689

static mrb_value
mrb_obj_missing(mrb_state *mrb, mrb_value mod)
{
  mrb_sym name;
  mrb_value *a;
  mrb_int alen;

  mrb_get_args(mrb, "n*!", &name, &a, &alen);
  mrb_method_missing(mrb, name, mod, mrb_ary_new_from_values(mrb, alen, a));
  /* not reached */
  return mrb_nil_value();
}

Class Method Details

.Array(arg) ⇒ Array

Returns +arg+ as an Array using to_a method.

Array(1..5) #=> [1, 2, 3, 4, 5]

Returns:



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'mrbgems/mruby-kernel-ext/src/kernel.c', line 168

static mrb_value
mrb_f_array(mrb_state *mrb, mrb_value self)
{
  mrb_value arg, tmp;

  mrb_get_args(mrb, "o", &arg);
  tmp = mrb_check_convert_type(mrb, arg, MRB_TT_ARRAY, "Array", "to_a");
  if (mrb_nil_p(tmp)) {
    return mrb_ary_new_from_values(mrb, 1, &arg);
  }

  return tmp;
}

.callerObject



7
8
9
10
11
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'mrbgems/mruby-kernel-ext/src/kernel.c', line 7

static mrb_value
mrb_f_caller(mrb_state *mrb, mrb_value self)
{
  mrb_value bt, v, length;
  mrb_int bt_len, argc, lev, n;

  bt = mrb_get_backtrace(mrb);
  bt_len = RARRAY_LEN(bt);
  argc = mrb_get_args(mrb, "|oo", &v, &length);

  switch (argc) {
    case 0:
      lev = 1;
      n = bt_len - lev;
      break;
    case 1:
      if (mrb_range_p(v)) {
        mrb_int beg, len;
        if (mrb_range_beg_len(mrb, v, &beg, &len, bt_len, TRUE) == MRB_RANGE_OK) {
          lev = beg;
          n = len;
        }
        else {
          return mrb_nil_value();
        }
      }
      else {
        lev = mrb_int(mrb, v);
        if (lev < 0) {
          mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative level (%v)", v);
        }
        n = bt_len - lev;
      }
      break;
    case 2:
      lev = mrb_int(mrb, v);
      n = mrb_int(mrb, length);
      if (lev < 0) {
        mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative level (%v)", v);
      }
      if (n < 0) {
        mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative size (%v)", length);
      }
      break;
    default:
      lev = n = 0;
      break;
  }

  if (n == 0) {
    return mrb_ary_new(mrb);
  }

  return mrb_funcall(mrb, bt, "[]", 2, mrb_fixnum_value(lev), mrb_fixnum_value(n));
}

.raiseObject .raise(string) ⇒ Object .raise(exception[, string]) ⇒ Object

With no arguments, raises a RuntimeError With a single +String+ argument, raises a +RuntimeError+ with the string as a message. Otherwise, the first parameter should be the name of an +Exception+ class (or an object that returns an +Exception+ object when sent an +exception+ message). The optional second parameter sets the message associated with the exception, and the third parameter is an array of callback information. Exceptions are caught by the +rescue+ clause of begin...end blocks.

raise “Failed to create socket” raise ArgumentError, “No parameters”, caller



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'src/kernel.c', line 585

MRB_API mrb_value
mrb_f_raise(mrb_state *mrb, mrb_value self)
{
  mrb_value a[2], exc;
  mrb_int argc;


  argc = mrb_get_args(mrb, "|oo", &a[0], &a[1]);
  switch (argc) {
  case 0:
    mrb_raise(mrb, E_RUNTIME_ERROR, "");
    break;
  case 1:
    if (mrb_string_p(a[0])) {
      a[1] = a[0];
      argc = 2;
      a[0] = mrb_obj_value(E_RUNTIME_ERROR);
    }
    /* fall through */
  default:
    exc = mrb_make_exception(mrb, argc, a);
    mrb_exc_raise(mrb, exc);
    break;
  }
  return mrb_nil_value();            /* not reached */
}

.Float(arg) ⇒ Float

Returns arg converted to a float. Numeric types are converted directly, the rest are converted using arg.to_f.

Float(1) #=> 1.0 Float(123.456) #=> 123.456 Float(“123.456”) #=> 123.456 Float(nil) #=> TypeError

Returns:



128
129
130
131
132
133
134
135
# File 'mrbgems/mruby-kernel-ext/src/kernel.c', line 128

static mrb_value
mrb_f_float(mrb_state *mrb, mrb_value self)
{
  mrb_value arg;

  mrb_get_args(mrb, "o", &arg);
  return mrb_Float(mrb, arg);
}

.Hash(arg) ⇒ Hash

Returns a Hash if arg is a Hash. Returns an empty Hash when arg is nil or [].

Hash([])          #=> {}
Hash(nil)         #=> {}
Hash(key: :value) #=> {:key => :value}
Hash([1, 2, 3])   #=> TypeError

Returns:



196
197
198
199
200
201
202
203
204
205
206
# File 'mrbgems/mruby-kernel-ext/src/kernel.c', line 196

static mrb_value
mrb_f_hash(mrb_state *mrb, mrb_value self)
{
  mrb_value arg;

  mrb_get_args(mrb, "o", &arg);
  if (mrb_nil_p(arg) || (mrb_array_p(arg) && RARRAY_LEN(arg) == 0)) {
    return mrb_hash_new(mrb);
  }
  return mrb_ensure_hash_type(mrb, arg);
}

.Integer(arg, base = 0) ⇒ Integer

Converts arg to a Fixnum. Numeric types are converted directly (with floating point numbers being truncated). base (0, or between 2 and 36) is a base for integer string representation. If arg is a String, when base is omitted or equals to zero, radix indicators (0, 0b, and 0x) are honored. In any case, strings should be strictly conformed to numeric representation. This behavior is different from that of String#to_i. Non string values will be treated as integers. Passing nil raises a TypeError.

Integer(123.999) #=> 123 Integer(“0x1a”) #=> 26 Integer(Time.new) #=> 1204973019 Integer(“0930”, 10) #=> 930 Integer(“111”, 2) #=> 7 Integer(nil) #=> TypeError

Returns:



105
106
107
108
109
110
111
112
113
# File 'mrbgems/mruby-kernel-ext/src/kernel.c', line 105

static mrb_value
mrb_f_integer(mrb_state *mrb, mrb_value self)
{
  mrb_value arg;
  mrb_int base = 0;

  mrb_get_args(mrb, "o|i", &arg, &base);
  return mrb_convert_to_integer(mrb, arg, base);
}

.String(arg) ⇒ String

Returns arg as an String. converted using to_s method.

String(self) #=> “main” String(self.class) #=> “Object” String(123456) #=> “123456”

Returns:



149
150
151
152
153
154
155
156
157
# File 'mrbgems/mruby-kernel-ext/src/kernel.c', line 149

static mrb_value
mrb_f_string(mrb_state *mrb, mrb_value self)
{
  mrb_value arg, tmp;

  mrb_get_args(mrb, "o", &arg);
  tmp = mrb_convert_type(mrb, arg, MRB_TT_STRING, "String", "to_s");
  return tmp;
}

Instance Method Details

#!~(y) ⇒ Object

11.4.4 Step c)



38
39
40
# File 'mrblib/kernel.rb', line 38

def !~(y)
  !(self =~ y)
end

#===(other) ⇒ Boolean

Case Equality—For class Object, effectively the same as calling #==, but typically overridden by descendants to provide meaningful semantics in case statements.

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
# File 'src/kernel.c', line 71

static mrb_value
mrb_equal_m(mrb_state *mrb, mrb_value self)
{
  mrb_value arg;

  mrb_get_args(mrb, "o", &arg);
  return mrb_bool_value(mrb_equal(mrb, self, arg));
}

#__case_eqqObject

internal



746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# File 'src/kernel.c', line 746

static mrb_value
mrb_obj_ceqq(mrb_state *mrb, mrb_value self)
{
  mrb_value v;
  mrb_int i, len;
  mrb_sym eqq = mrb_intern_lit(mrb, "===");
  mrb_value ary = mrb_ary_splat(mrb, self);

  mrb_get_args(mrb, "o", &v);
  len = RARRAY_LEN(ary);
  for (i=0; i<len; i++) {
    mrb_value c = mrb_funcall_argv(mrb, mrb_ary_entry(ary, i), eqq, 1, &v);
    if (mrb_test(c)) return mrb_true_value();
  }
  return mrb_false_value();
}

#__method__Object

Returns the name at the definition of the current method as a Symbol. If called outside of a method, it returns nil.



72
73
74
75
76
77
78
79
80
81
# File 'mrbgems/mruby-kernel-ext/src/kernel.c', line 72

static mrb_value
mrb_f_method(mrb_state *mrb, mrb_value self)
{
  mrb_callinfo *ci = mrb->c->ci;
  ci--;
  if (ci->mid)
    return mrb_symbol_value(ci->mid);
  else
    return mrb_nil_value();
}

#__printstr__Object

15.3.1.3.34



41
42
43
44
45
46
47
48
49
50
# File 'mrbgems/mruby-print/src/print.c', line 41

mrb_value
mrb_printstr(mrb_state *mrb, mrb_value self)
{
  mrb_value argv;

  mrb_get_args(mrb, "o", &argv);
  printstr(mrb, argv);

  return argv;
}

#__to_intObject

internal

#__to_strObject

internal

#_inspect(_recur_list) ⇒ Object

internal method for inspect



43
44
45
# File 'mrblib/kernel.rb', line 43

def _inspect(_recur_list)
  self.inspect
end

#`(cmd) ⇒ Object

15.3.1.2.1 Kernel. provided by Kernel# 15.3.1.3.3



10
11
12
# File 'mrblib/kernel.rb', line 10

def `(s)
  raise NotImplementedError.new("backquotes not implemented")
end

#block_given?Boolean #iterator?Boolean

Returns true if yield would execute a block in the current context. The iterator? form is mildly deprecated.

def try if block_given? yield else “no block” end end try #=> “no block” try { “hello” } #=> “hello” try do “hello” end #=> “hello”

Overloads:

  • #block_given?Boolean

    Returns:

    • (Boolean)
  • #iterator?Boolean

    Returns:

    • (Boolean)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'src/kernel.c', line 127

static mrb_value
mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self)
{
  mrb_callinfo *ci = &mrb->c->ci[-1];
  mrb_callinfo *cibase = mrb->c->cibase;
  mrb_value *bp;
  struct RProc *p;

  if (ci <= cibase) {
    /* toplevel does not have block */
    return mrb_false_value();
  }
  p = ci->proc;
  /* search method/class/module proc */
  while (p) {
    if (MRB_PROC_SCOPE_P(p)) break;
    p = p->upper;
  }
  if (p == NULL) return mrb_false_value();
  /* search ci corresponding to proc */
  while (cibase < ci) {
    if (ci->proc == p) break;
    ci--;
  }
  if (ci == cibase) {
    return mrb_false_value();
  }
  else if (ci->env) {
    struct REnv *e = ci->env;
    int bidx;

    /* top-level does not have block slot (always false) */
    if (e->stack == mrb->c->stbase)
      return mrb_false_value();
    /* use saved block arg position */
    bidx = MRB_ENV_BIDX(e);
    /* bidx may be useless (e.g. define_method) */
    if (bidx >= MRB_ENV_STACK_LEN(e))
      return mrb_false_value();
    bp = &e->stack[bidx];
  }
  else {
    bp = ci[1].stackent+1;
    if (ci->argc >= 0) {
      bp += ci->argc;
    }
    else {
      bp++;
    }
  }
  if (mrb_nil_p(*bp))
    return mrb_false_value();
  return mrb_true_value();
}

#classClass

Returns the class of obj. This method must always be called with an explicit receiver, as class is also a reserved word in Ruby.

1.class #=> Fixnum self.class #=> Object

Returns:

  • (Class)


194
195
196
197
198
# File 'src/kernel.c', line 194

static mrb_value
mrb_obj_class_m(mrb_state *mrb, mrb_value self)
{
  return mrb_obj_value(mrb_obj_class(mrb, self));
}

#cloneObject

Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. Copies the frozen state of obj. See also the discussion under Object#dup.

class Klass attr_accessor :str end s1 = Klass.new #=> # s1.str = "Hello" #=> "Hello" s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello"> s2.str[1,4] = "i" #=> "i" s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">" s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"

This method may have class-specific behavior. If so, that behavior will be documented under the #+initialize_copy+ method of the class.

Some Class(True False Nil Symbol Fixnum Float) Object cannot clone.

Returns:

  • (Object)


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'src/kernel.c', line 321

MRB_API mrb_value
mrb_obj_clone(mrb_state *mrb, mrb_value self)
{
  struct RObject *p;
  mrb_value clone;

  if (mrb_immediate_p(self)) {
    mrb_raisef(mrb, E_TYPE_ERROR, "can't clone %v", self);
  }
  if (mrb_sclass_p(self)) {
    mrb_raise(mrb, E_TYPE_ERROR, "can't clone singleton class");
  }
  p = (struct RObject*)mrb_obj_alloc(mrb, mrb_type(self), mrb_obj_class(mrb, self));
  p->c = mrb_singleton_class_clone(mrb, self);
  mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)p->c);
  clone = mrb_obj_value(p);
  init_copy(mrb, clone, self);
  p->flags |= mrb_obj_ptr(self)->flags & MRB_FL_OBJ_IS_FROZEN;

  return clone;
}

#define_singleton_methodObject

15.3.1.3.45



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'mrbgems/mruby-metaprog/src/metaprog.c', line 384

static mrb_value
mod_define_singleton_method(mrb_state *mrb, mrb_value self)
{
  struct RProc *p;
  mrb_method_t m;
  mrb_sym mid;
  mrb_value blk = mrb_nil_value();

  mrb_get_args(mrb, "n&!", &mid, &blk);
  p = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb->proc_class);
  mrb_proc_copy(p, mrb_proc_ptr(blk));
  p->flags |= MRB_PROC_STRICT;
  MRB_METHOD_FROM_PROC(m, p);
  mrb_define_method_raw(mrb, mrb_class_ptr(mrb_singleton_class(mrb, self)), mid, m);
  return mrb_symbol_value(mid);
}

#dupObject

Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. dup copies the frozen state of obj. See also the discussion under Object#clone. In general, clone and dup may have different semantics in descendant classes. While clone is used to duplicate an object, including its internal state, dup typically uses the class of the descendant object to create the new instance.

This method may have class-specific behavior. If so, that behavior will be documented under the #+initialize_copy+ method of the class.

Returns:

  • (Object)


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

MRB_API mrb_value
mrb_obj_dup(mrb_state *mrb, mrb_value obj)
{
  struct RBasic *p;
  mrb_value dup;

  if (mrb_immediate_p(obj)) {
    mrb_raisef(mrb, E_TYPE_ERROR, "can't dup %v", obj);
  }
  if (mrb_sclass_p(obj)) {
    mrb_raise(mrb, E_TYPE_ERROR, "can't dup singleton class");
  }
  p = mrb_obj_alloc(mrb, mrb_type(obj), mrb_obj_class(mrb, obj));
  dup = mrb_obj_value(p);
  init_copy(mrb, dup, obj);

  return dup;
}

#==(other) ⇒ Boolean #equal?(other) ⇒ Boolean #eql?(other) ⇒ Boolean

Equality—At the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendant classes to provide class-specific meaning.

Unlike ==, the equal? method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b) iff a is the same object as b).

The eql? method returns true if obj and anObject have the same value. Used by Hash to test members for equality. For objects of class Object, eql? is synonymous with ==. Subclasses normally continue this tradition, but there are exceptions. Numeric types, for example, perform type conversion across ==, but not across eql?, so:

1 == 1.0 #=> true 1.eql? 1.0 #=> false

Overloads:

  • #==(other) ⇒ Boolean

    Returns:

    • (Boolean)
  • #equal?(other) ⇒ Boolean

    Returns:

    • (Boolean)
  • #eql?(other) ⇒ Boolean

    Returns:

    • (Boolean)


1670
1671
1672
1673
1674
1675
1676
1677
# File 'src/class.c', line 1670

mrb_value
mrb_obj_equal_m(mrb_state *mrb, mrb_value self)
{
  mrb_value arg;

  mrb_get_args(mrb, "o", &arg);
  return mrb_bool_value(mrb_obj_equal(mrb, self, arg));
}

#extendObject

Adds to obj the instance methods from each module given as a parameter.

module Mod def hello “Hello from Mod.\n” end end

class Klass def hello “Hello from Klass.\n” end end

k = Klass.new k.hello #=> “Hello from Klass.\n” k.extend(Mod) #=> # k.hello #=> "Hello from Mod.\n"

Returns:

  • (Object)


424
425
426
427
428
429
430
431
432
# File 'src/kernel.c', line 424

static mrb_value
mrb_obj_extend_m(mrb_state *mrb, mrb_value self)
{
  mrb_value *argv;
  mrb_int argc;

  mrb_get_args(mrb, "*", &argv, &argc);
  return mrb_obj_extend(mrb, argc, argv, self);
}

#formatObject

in sprintf.c



9
# File 'mrbgems/mruby-sprintf/src/kernel.c', line 9

mrb_value mrb_f_sprintf(mrb_state *mrb, mrb_value obj);

#freezeObject

15.3.1.3.13



434
435
436
437
438
439
440
441
442
443
444
445
# File 'src/kernel.c', line 434

MRB_API mrb_value
mrb_obj_freeze(mrb_state *mrb, mrb_value self)
{
  if (!mrb_immediate_p(self)) {
    struct RBasic *b = mrb_basic_ptr(self);
    if (!mrb_frozen_p(b)) {
      MRB_SET_FROZEN_FLAG(b);
      if (b->c->tt == MRB_TT_SCLASS) MRB_SET_FROZEN_FLAG(b->c);
    }
  }
  return self;
}

#frozen?Boolean

Returns:

  • (Boolean)


447
448
449
450
451
# File 'src/kernel.c', line 447

static mrb_value
mrb_obj_frozen(mrb_state *mrb, mrb_value self)
{
  return mrb_bool_value(mrb_immediate_p(self) || mrb_frozen_p(mrb_basic_ptr(self)));
}

#gets(*args) ⇒ Object



28
29
30
# File 'mrbgems/mruby-io/mrblib/kernel.rb', line 28

def gets(*args)
  $stdin.gets(*args)
end

#global_variablesArray

Returns an array of the names of global variables.

global_variables.grep /std/ #=> [:$stdin, :$stdout, :$stderr]

Returns:



985
986
987
988
989
990
991
992
993
# File 'src/variable.c', line 985

mrb_value
mrb_f_global_variables(mrb_state *mrb, mrb_value self)
{
  iv_tbl *t = mrb->globals;
  mrb_value ary = mrb_ary_new(mrb);

  iv_foreach(mrb, t, gv_i, &ary);
  return ary;
}

#hashFixnum

Generates a Fixnum hash value for this object. This function must have the property that a.eql?(b) implies a.hash == b.hash. The hash value is used by class Hash. Any hash value that exceeds the capacity of a Fixnum will be truncated before being used.

Returns:



464
465
466
467
468
# File 'src/kernel.c', line 464

static mrb_value
mrb_obj_hash(mrb_state *mrb, mrb_value self)
{
  return mrb_fixnum_value(mrb_obj_id(self));
}

#initialize_copyObject

15.3.1.3.16



471
472
473
474
475
476
477
478
479
480
481
482
# File 'src/kernel.c', line 471

static mrb_value
mrb_obj_init_copy(mrb_state *mrb, mrb_value self)
{
  mrb_value orig;

  mrb_get_args(mrb, "o", &orig);
  if (mrb_obj_equal(mrb, self, orig)) return self;
  if ((mrb_type(self) != mrb_type(orig)) || (mrb_obj_class(mrb, self) != mrb_obj_class(mrb, orig))) {
      mrb_raise(mrb, E_TYPE_ERROR, "initialize_copy should take same class object");
  }
  return self;
}

#inspectString

Returns a string containing a human-readable representation of obj. If not overridden and no instance variables, uses the to_s method to generate the string. obj. If not overridden, uses the to_s method to generate the string.

[ 1, 2, 3..4, ‘five’ ].inspect #=> “[1, 2, 3..4, "five"]” Time.new.inspect #=> “2008-03-08 19:43:39 +0900”

Returns:



53
54
55
56
57
58
59
60
# File 'src/kernel.c', line 53

MRB_API mrb_value
mrb_obj_inspect(mrb_state *mrb, mrb_value obj)
{
  if (mrb_object_p(obj) && mrb_obj_basic_to_s_p(mrb, obj)) {
    return mrb_obj_iv_inspect(mrb, mrb_obj_ptr(obj));
  }
  return mrb_any_to_s(mrb, obj);
}

#instance_of?Boolean

Returns true if obj is an instance of the given class. See also Object#kind_of?.

Returns:

  • (Boolean)


500
501
502
503
504
505
506
507
508
# File 'src/kernel.c', line 500

static mrb_value
obj_is_instance_of(mrb_state *mrb, mrb_value self)
{
  mrb_value arg;

  mrb_get_args(mrb, "C", &arg);

  return mrb_bool_value(mrb_obj_is_instance_of(mrb, self, mrb_class_ptr(arg)));
}

#instance_variable_defined?(symbol) ⇒ Boolean

Returns true if the given instance variable is defined in obj.

class Fred def initialize(p1, p2) @a, @b = p1, p2 end end fred = Fred.new(‘cat’, 99) fred.instance_variable_defined?(:@a) #=> true fred.instance_variable_defined?(“@b”) #=> true fred.instance_variable_defined?(“@c”) #=> false

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
# File 'mrbgems/mruby-metaprog/src/metaprog.c', line 47

static mrb_value
mrb_obj_ivar_defined(mrb_state *mrb, mrb_value self)
{
  mrb_sym sym;

  mrb_get_args(mrb, "n", &sym);
  mrb_iv_name_sym_check(mrb, sym);
  return mrb_bool_value(mrb_iv_defined(mrb, self, sym));
}

#instance_variable_get(symbol) ⇒ Object

Returns the value of the given instance variable, or nil if the instance variable is not set. The @ part of the variable name should be included for regular instance variables. Throws a NameError exception if the supplied symbol is not valid as an instance variable name.

class Fred def initialize(p1, p2) @a, @b = p1, p2 end end fred = Fred.new(‘cat’, 99) fred.instance_variable_get(:@a) #=> “cat” fred.instance_variable_get(“@b”) #=> 99

Returns:

  • (Object)


77
78
79
80
81
82
83
84
85
# File 'mrbgems/mruby-metaprog/src/metaprog.c', line 77

static mrb_value
mrb_obj_ivar_get(mrb_state *mrb, mrb_value self)
{
  mrb_sym iv_name;

  mrb_get_args(mrb, "n", &iv_name);
  mrb_iv_name_sym_check(mrb, iv_name);
  return mrb_iv_get(mrb, self, iv_name);
}

#instance_variable_set(symbol, obj) ⇒ Object

Sets the instance variable names by symbol to object, thereby frustrating the efforts of the class’s author to attempt to provide proper encapsulation. The variable did not have to exist prior to this call.

class Fred def initialize(p1, p2) @a, @b = p1, p2 end end fred = Fred.new(‘cat’, 99) fred.instance_variable_set(:@a, ‘dog’) #=> “dog” fred.instance_variable_set(:@c, ‘cat’) #=> “cat” fred.inspect #=> “#<Fred:0x401b3da8 @a="dog", @b=99, @c="cat">”

Returns:

  • (Object)


107
108
109
110
111
112
113
114
115
116
117
# File 'mrbgems/mruby-metaprog/src/metaprog.c', line 107

static mrb_value
mrb_obj_ivar_set(mrb_state *mrb, mrb_value self)
{
  mrb_sym iv_name;
  mrb_value val;

  mrb_get_args(mrb, "no", &iv_name, &val);
  mrb_iv_name_sym_check(mrb, iv_name);
  mrb_iv_set(mrb, self, iv_name, val);
  return val;
}

#instance_variablesArray

Returns an array of instance variable names for the receiver. Note that simply defining an accessor does not create the corresponding instance variable.

class Fred attr_accessor :a1 def initialize @iv = 3 end end Fred.new.instance_variables #=> [:@iv]

Returns:



568
569
570
571
572
573
574
575
576
577
578
# File 'src/variable.c', line 568

mrb_value
mrb_obj_instance_variables(mrb_state *mrb, mrb_value self)
{
  mrb_value ary;

  ary = mrb_ary_new(mrb);
  if (obj_iv_p(self)) {
    iv_foreach(mrb, mrb_obj_ptr(self)->iv, iv_i, &ary);
  }
  return ary;
}

#is_a?Boolean #kind_of?Boolean

Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.

module M; end class A include M end class B < A; end class C < B; end b = B.new b.instance_of? A #=> false b.instance_of? B #=> true b.instance_of? C #=> false b.instance_of? M #=> false b.kind_of? A #=> true b.kind_of? B #=> true b.kind_of? C #=> false b.kind_of? M #=> true

Overloads:

  • #is_a?Boolean

    Returns:

    • (Boolean)
  • #kind_of?Boolean

    Returns:

    • (Boolean)


537
538
539
540
541
542
543
544
545
# File 'src/kernel.c', line 537

static mrb_value
mrb_obj_is_kind_of_m(mrb_state *mrb, mrb_value self)
{
  mrb_value arg;

  mrb_get_args(mrb, "C", &arg);

  return mrb_bool_value(mrb_obj_is_kind_of(mrb, self, mrb_class_ptr(arg)));
}

#block_given?Boolean #iterator?Boolean

Returns true if yield would execute a block in the current context. The iterator? form is mildly deprecated.

def try if block_given? yield else “no block” end end try #=> “no block” try { “hello” } #=> “hello” try do “hello” end #=> “hello”

Overloads:

  • #block_given?Boolean

    Returns:

    • (Boolean)
  • #iterator?Boolean

    Returns:

    • (Boolean)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'src/kernel.c', line 127

static mrb_value
mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self)
{
  mrb_callinfo *ci = &mrb->c->ci[-1];
  mrb_callinfo *cibase = mrb->c->cibase;
  mrb_value *bp;
  struct RProc *p;

  if (ci <= cibase) {
    /* toplevel does not have block */
    return mrb_false_value();
  }
  p = ci->proc;
  /* search method/class/module proc */
  while (p) {
    if (MRB_PROC_SCOPE_P(p)) break;
    p = p->upper;
  }
  if (p == NULL) return mrb_false_value();
  /* search ci corresponding to proc */
  while (cibase < ci) {
    if (ci->proc == p) break;
    ci--;
  }
  if (ci == cibase) {
    return mrb_false_value();
  }
  else if (ci->env) {
    struct REnv *e = ci->env;
    int bidx;

    /* top-level does not have block slot (always false) */
    if (e->stack == mrb->c->stbase)
      return mrb_false_value();
    /* use saved block arg position */
    bidx = MRB_ENV_BIDX(e);
    /* bidx may be useless (e.g. define_method) */
    if (bidx >= MRB_ENV_STACK_LEN(e))
      return mrb_false_value();
    bp = &e->stack[bidx];
  }
  else {
    bp = ci[1].stackent+1;
    if (ci->argc >= 0) {
      bp += ci->argc;
    }
    else {
      bp++;
    }
  }
  if (mrb_nil_p(*bp))
    return mrb_false_value();
  return mrb_true_value();
}

#is_a?Boolean #kind_of?Boolean

Returns true if class is the class of obj, or if class is one of the superclasses of obj or modules included in obj.

module M; end class A include M end class B < A; end class C < B; end b = B.new b.instance_of? A #=> false b.instance_of? B #=> true b.instance_of? C #=> false b.instance_of? M #=> false b.kind_of? A #=> true b.kind_of? B #=> true b.kind_of? C #=> false b.kind_of? M #=> true

Overloads:

  • #is_a?Boolean

    Returns:

    • (Boolean)
  • #kind_of?Boolean

    Returns:

    • (Boolean)


537
538
539
540
541
542
543
544
545
# File 'src/kernel.c', line 537

static mrb_value
mrb_obj_is_kind_of_m(mrb_state *mrb, mrb_value self)
{
  mrb_value arg;

  mrb_get_args(mrb, "C", &arg);

  return mrb_bool_value(mrb_obj_is_kind_of(mrb, self, mrb_class_ptr(arg)));
}

#local_variablesArray

Returns the names of local variables in the current scope.

[mruby limitation] If variable symbol information was stripped out from compiled binary files using mruby-strip -l, this method always returns an empty array.

Returns:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'mrbgems/mruby-metaprog/src/metaprog.c', line 132

static mrb_value
mrb_local_variables(mrb_state *mrb, mrb_value self)
{
  struct RProc *proc;
  mrb_irep *irep;
  mrb_value vars;
  size_t i;

  proc = mrb->c->ci[-1].proc;

  if (MRB_PROC_CFUNC_P(proc)) {
    return mrb_ary_new(mrb);
  }
  vars = mrb_hash_new(mrb);
  while (proc) {
    if (MRB_PROC_CFUNC_P(proc)) break;
    irep = proc->body.irep;
    if (!irep->lv) break;
    for (i = 0; i + 1 < irep->nlocals; ++i) {
      if (irep->lv[i].name) {
        mrb_sym sym = irep->lv[i].name;
        const char *name = mrb_sym_name(mrb, sym);
        switch (name[0]) {
        case '*': case '&':
          break;
        default:
          mrb_hash_set(mrb, vars, mrb_symbol_value(sym), mrb_true_value());
          break;
        }
      }
    }
    if (!MRB_PROC_ENV_P(proc)) break;
    proc = proc->upper;
    //if (MRB_PROC_SCOPE_P(proc)) break;
    if (!proc->c) break;
  }

  return mrb_hash_keys(mrb, vars);
}

#loop(&block) ⇒ Object

Calls the given block repetitively.

ISO 15.3.1.3.29



27
28
29
30
31
32
33
34
35
# File 'mrblib/kernel.rb', line 27

def loop(&block)
  return to_enum :loop unless block

  while true
    yield
  end
rescue StopIteration => e
  e.result
end

#methodsArray

Returns a list of the names of methods publicly accessible in obj. This will include all the methods accessible in obj’s ancestors.

class Klass def kMethod() end end k = Klass.new k.methods[0..9] #=> [:kMethod, :respond_to?, :nil?, :is_a?, # :class, :instance_variable_set, # :methods, :extend, :send, :instance_eval] k.methods.length #=> 42

Returns:



252
253
254
255
256
257
258
# File 'mrbgems/mruby-metaprog/src/metaprog.c', line 252

static mrb_value
mrb_obj_methods_m(mrb_state *mrb, mrb_value self)
{
  mrb_bool recur = TRUE;
  mrb_get_args(mrb, "|b", &recur);
  return mrb_obj_methods(mrb, recur, self, (mrb_method_flag_t)0); /* everything but private */
}

#nil?Boolean

call_seq: nil.nil? -> true

.nil? -> false Only the object nil responds true to nil?.

Returns:

  • (Boolean)


547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'src/kernel.c', line 547

KHASH_DECLARE(st, mrb_sym, char, FALSE)
KHASH_DEFINE(st, mrb_sym, char, FALSE, kh_int_hash_func, kh_int_hash_equal)

/* 15.3.1.3.32 */
/*
 * call_seq:
 *   nil.nil?               -> true
 *   <anything_else>.nil?   -> false
 *
 * Only the object <i>nil</i> responds <code>true</code> to <code>nil?</code>.
 */
static mrb_value
mrb_false(mrb_state *mrb, mrb_value self)
{
  return mrb_false_value();
}

#object_idObject

call-seq: obj.id -> fixnum obj.object_id -> fixnum

Returns an integer identifier for obj. The same number will be returned on all calls to id for a given object, and no two active objects will share an id. Object#object_id is a different concept from the :name notation, which returns the symbol id of name. Replaces the deprecated Object#id.



97
98
99
100
101
# File 'src/kernel.c', line 97

mrb_value
mrb_obj_id_m(mrb_state *mrb, mrb_value self)
{
  return mrb_fixnum_value(mrb_obj_id(self));
}

#open(file, *rest, &block) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
# File 'mrbgems/mruby-io/mrblib/kernel.rb', line 6

def open(file, *rest, &block)
  raise ArgumentError unless file.is_a?(String)

  if file[0] == "|"
    IO.popen(file[1..-1], *rest, &block)
  else
    File.open(file, *rest, &block)
  end
end

#p(*args) ⇒ Object

Print human readable object description

ISO 15.3.1.3.34



40
41
42
43
44
45
46
47
48
49
# File 'mrbgems/mruby-print/mrblib/print.rb', line 40

def p(*args)
  i = 0
  len = args.size
  while i < len
    __printstr__ args[i].inspect
    __printstr__ "\n"
    i += 1
  end
  args.__svalue
end

Invoke method +print+ on STDOUT and passing +*args+

ISO 15.3.1.2.10



10
11
12
# File 'mrbgems/mruby-print/mrblib/print.rb', line 10

def print(*args)
  $stdout.print(*args)
end

#printf(*args) ⇒ Object



24
25
26
# File 'mrbgems/mruby-io/mrblib/kernel.rb', line 24

def printf(*args)
  $stdout.printf(*args)
end

#private_methods(all = true) ⇒ Array

Returns the list of private methods accessible to obj. If the all parameter is set to false, only those methods in the receiver will be listed.

Returns:



269
270
271
272
273
274
275
# File 'mrbgems/mruby-metaprog/src/metaprog.c', line 269

static mrb_value
mrb_obj_private_methods(mrb_state *mrb, mrb_value self)
{
  mrb_bool recur = TRUE;
  mrb_get_args(mrb, "|b", &recur);
  return mrb_obj_methods(mrb, recur, self, NOEX_PRIVATE); /* private attribute not define */
}

#protected_methods(all = true) ⇒ Array

Returns the list of protected methods accessible to obj. If the all parameter is set to false, only those methods in the receiver will be listed.

Returns:



286
287
288
289
290
291
292
# File 'mrbgems/mruby-metaprog/src/metaprog.c', line 286

static mrb_value
mrb_obj_protected_methods(mrb_state *mrb, mrb_value self)
{
  mrb_bool recur = TRUE;
  mrb_get_args(mrb, "|b", &recur);
  return mrb_obj_methods(mrb, recur, self, NOEX_PROTECTED); /* protected attribute not define */
}

#public_methods(all = true) ⇒ Array

Returns the list of public methods accessible to obj. If the all parameter is set to false, only those methods in the receiver will be listed.

Returns:



303
304
305
306
307
308
309
# File 'mrbgems/mruby-metaprog/src/metaprog.c', line 303

static mrb_value
mrb_obj_public_methods(mrb_state *mrb, mrb_value self)
{
  mrb_bool recur = TRUE;
  mrb_get_args(mrb, "|b", &recur);
  return mrb_obj_methods(mrb, recur, self, NOEX_PUBLIC); /* public attribute not define */
}

#puts(*args) ⇒ Object

Invoke method +puts+ on STDOUT and passing +args+

ISO 15.3.1.2.11



23
24
25
# File 'mrbgems/mruby-print/mrblib/print.rb', line 23

def puts(*args)
  $stdout.puts(*args)
end

#raiseObject #raise(string) ⇒ Object #raise(exception[, string]) ⇒ Object

With no arguments, raises a RuntimeError With a single +String+ argument, raises a +RuntimeError+ with the string as a message. Otherwise, the first parameter should be the name of an +Exception+ class (or an object that returns an +Exception+ object when sent an +exception+ message). The optional second parameter sets the message associated with the exception, and the third parameter is an array of callback information. Exceptions are caught by the +rescue+ clause of begin...end blocks.

raise “Failed to create socket” raise ArgumentError, “No parameters”, caller



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'src/kernel.c', line 585

MRB_API mrb_value
mrb_f_raise(mrb_state *mrb, mrb_value self)
{
  mrb_value a[2], exc;
  mrb_int argc;


  argc = mrb_get_args(mrb, "|oo", &a[0], &a[1]);
  switch (argc) {
  case 0:
    mrb_raise(mrb, E_RUNTIME_ERROR, "");
    break;
  case 1:
    if (mrb_string_p(a[0])) {
      a[1] = a[0];
      argc = 2;
      a[0] = mrb_obj_value(E_RUNTIME_ERROR);
    }
    /* fall through */
  default:
    exc = mrb_make_exception(mrb, argc, a);
    mrb_exc_raise(mrb, exc);
    break;
  }
  return mrb_nil_value();            /* not reached */
}

#Rational(numerator, denominator = 1) ⇒ Object



86
87
88
89
90
91
# File 'mrbgems/mruby-rational/mrblib/rational.rb', line 86

def Rational(numerator, denominator = 1)
  a = numerator
  b = denominator
  a, b = b, a % b until b == 0
  Rational._new(numerator.div(a), denominator.div(a))
end

#remove_instance_variable(symbol) ⇒ Object

Removes the named instance variable from obj, returning that variable’s value.

class Dummy attr_reader :var def initialize @var = 99 end def remove remove_instance_variable(:@var) end end d = Dummy.new d.var #=> 99 d.remove #=> 99 d.var #=> nil

Returns:

  • (Object)


634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'src/kernel.c', line 634

static mrb_value
mrb_obj_remove_instance_variable(mrb_state *mrb, mrb_value self)
{
  mrb_sym sym;
  mrb_value val;

  mrb_get_args(mrb, "n", &sym);
  mrb_iv_name_sym_check(mrb, sym);
  val = mrb_iv_remove(mrb, self, sym);
  if (mrb_undef_p(val)) {
    mrb_name_error(mrb, sym, "instance variable %n not defined", sym);
  }
  return val;
}

#respond_to?(symbol, include_private = false) ⇒ Boolean

Returns +true+ if obj responds to the given method. Private methods are included in the search only if the optional second parameter evaluates to +true+.

If the method is not implemented, as Process.fork on Windows, File.lchmod on GNU/Linux, etc., false is returned.

If the method is not defined, respond_to_missing? method is called and the result is returned.

Returns:

  • (Boolean)


725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'src/kernel.c', line 725

static mrb_value
obj_respond_to(mrb_state *mrb, mrb_value self)
{
  mrb_sym id, rtm_id;
  mrb_bool priv = FALSE, respond_to_p;

  mrb_get_args(mrb, "n|b", &id, &priv);
  respond_to_p = basic_obj_respond_to(mrb, self, id, !priv);
  if (!respond_to_p) {
    rtm_id = mrb_intern_lit(mrb, "respond_to_missing?");
    if (basic_obj_respond_to(mrb, self, rtm_id, !priv)) {
      mrb_value args[2], v;
      args[0] = mrb_symbol_value(id);
      args[1] = mrb_bool_value(priv);
      v = mrb_funcall_argv(mrb, self, rtm_id, 2, args);
      return mrb_bool_value(mrb_bool(v));
    }
  }
  return mrb_bool_value(respond_to_p);
}

#send(symbol[, args...]) ⇒ Object #__send__(symbol[, args...]) ⇒ Object

Invokes the method identified by symbol, passing it any arguments specified. You can use __send__ if the name +send+ clashes with an existing method in obj.

class Klass def hello(*args) “Hello “ + args.join(‘ ‘) end end k = Klass.new k.send :hello, “gentle”, “readers” #=> “Hello gentle readers”

Overloads:

  • #send(symbol[, args...]) ⇒ Object

    Returns:

    • (Object)
  • #__send__(symbol[, args...]) ⇒ Object

    Returns:

    • (Object)


592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'src/vm.c', line 592

mrb_value
mrb_f_send(mrb_state *mrb, mrb_value self)
{
  mrb_sym name;
  mrb_value block, *argv, *regs;
  mrb_int argc, i, len;
  mrb_method_t m;
  struct RClass *c;
  mrb_callinfo *ci;

  mrb_get_args(mrb, "n*&", &name, &argv, &argc, &block);
  ci = mrb->c->ci;
  if (ci->acc < 0) {
  funcall:
    return mrb_funcall_with_block(mrb, self, name, argc, argv, block);
  }

  c = mrb_class(mrb, self);
  m = mrb_method_search_vm(mrb, &c, name);
  if (MRB_METHOD_UNDEF_P(m)) {            /* call method_mising */
    goto funcall;
  }

  ci->mid = name;
  ci->target_class = c;
  regs = mrb->c->stack+1;
  /* remove first symbol from arguments */
  if (ci->argc >= 0) {
    for (i=0,len=ci->argc; i<len; i++) {
      regs[i] = regs[i+1];
    }
    ci->argc--;
  }
  else {                     /* variable length arguments */
    mrb_ary_shift(mrb, regs[0]);
  }

  if (MRB_METHOD_CFUNC_P(m)) {
    if (MRB_METHOD_PROC_P(m)) {
      ci->proc = MRB_METHOD_PROC(m);
    }
    return MRB_METHOD_CFUNC(m)(mrb, self);
  }
  return mrb_exec_irep(mrb, self, MRB_METHOD_PROC(m));
}

#singleton_classObject

15.3.1.3.28 (15.3.1.2.7)



1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
# File 'src/class.c', line 1264

MRB_API mrb_value
mrb_singleton_class(mrb_state *mrb, mrb_value v)
{
  struct RBasic *obj;

  switch (mrb_type(v)) {
  case MRB_TT_FALSE:
    if (mrb_nil_p(v))
      return mrb_obj_value(mrb->nil_class);
    return mrb_obj_value(mrb->false_class);
  case MRB_TT_TRUE:
    return mrb_obj_value(mrb->true_class);
  case MRB_TT_CPTR:
    return mrb_obj_value(mrb->object_class);
  case MRB_TT_SYMBOL:
  case MRB_TT_FIXNUM:
#ifndef MRB_WITHOUT_FLOAT
  case MRB_TT_FLOAT:
#endif
    mrb_raise(mrb, E_TYPE_ERROR, "can't define singleton");
    return mrb_nil_value();    /* not reached */
  default:
    break;
  }
  obj = mrb_basic_ptr(v);
  prepare_singleton_class(mrb, obj);
  return mrb_obj_value(obj->c);
}

#singleton_method(name) ⇒ Object



2
3
4
5
6
7
8
9
# File 'mrbgems/mruby-method/mrblib/kernel.rb', line 2

def singleton_method(name)
  m = method(name)
  sc = (class <<self; self; end)
  if m.owner != sc
    raise NameError, "undefined method '#{name}' for class '#{sc}'"
  end
  m
end

#singleton_methods(all = true) ⇒ Array

Returns an array of the names of singleton methods for obj. If the optional all parameter is true, the list will include methods in modules included in obj. Only public and protected singleton methods are returned.

module Other def three() end end

class Single def Single.four() end end

a = Single.new

def a.one() end

class « a include Other def two() end end

Single.singleton_methods #=> [:four] a.singleton_methods(false) #=> [:two, :one] a.singleton_methods #=> [:two, :one, :three]

Returns:



376
377
378
379
380
381
382
# File 'mrbgems/mruby-metaprog/src/metaprog.c', line 376

static mrb_value
mrb_obj_singleton_methods_m(mrb_state *mrb, mrb_value self)
{
  mrb_bool recur = TRUE;
  mrb_get_args(mrb, "|b", &recur);
  return mrb_obj_singleton_methods(mrb, recur, self);
}

#sprintfObject

in sprintf.c



9
# File 'mrbgems/mruby-sprintf/src/kernel.c', line 9

mrb_value mrb_f_sprintf(mrb_state *mrb, mrb_value obj);

#tap {|_self| ... } ⇒ Object

call-seq: obj.tap{|x|…} -> obj

Yields x to the block, and then returns x. The primary purpose of this method is to “tap into” a method chain, in order to perform operations on intermediate results within the chain.

(1..10) .tap x puts “original: #{x.inspect”}    
.to_a .tap x puts “array: #{x.inspect”}    
.select x x%2==0 .tap x puts “evens: #{x.inspect”}
.map { x x*x } .tap x puts “squares: #{x.inspect”}

Yields:

  • (_self)

Yield Parameters:

  • _self (Kernel)

    the object that the method was called on



29
30
31
32
# File 'mrbgems/mruby-object-ext/mrblib/object.rb', line 29

def tap
  yield self
  self
end

#to_enum(meth = :each, *args) ⇒ Object Also known as: enum_for

call-seq: obj.to_enum(method = :each, *args) -> enum obj.enum_for(method = :each, *args) -> enum

Creates a new Enumerator which will enumerate by calling +method+ on +obj+, passing +args+ if any.

=== Examples

str = “xyz”

enum = str.enum_for(:each_byte) enum.each { |b| puts b } # => 120 # => 121 # => 122

# protect an array from being modified by some_method a = [1, 2, 3] some_method(a.to_enum)

It is typical to call to_enum when defining methods for a generic Enumerable, in case no block is passed.

Here is such an example with parameter passing:

module Enumerable
  # a generic method to repeat the values of any enumerable
  def repeat(n)
    raise ArgumentError, "#{n} is negative!" if n < 0
    unless block_given?
      return to_enum(__method__, n) # __method__ is :repeat here
    end
    each do |*val|
      n.times { yield *val }
    end
  end
end

%i[hello world].repeat(2) { |w| puts w }
  # => Prints 'hello', 'hello', 'world', 'world'
enum = (1..14).repeat(3)
  # => returns an Enumerator when called without a block
enum.first(4) # => [1, 1, 1, 2]


647
648
649
# File 'mrbgems/mruby-enumerator/mrblib/enumerator.rb', line 647

def to_enum(*a)
  raise NotImplementedError.new("fiber required for enumerator")
end

#to_sObject

15.3.1.3.46

#yield_self(&block) ⇒ Object Also known as: then

call-seq: obj.yield_self {|_obj|…} -> an_object obj.then {|_obj|…} -> an_object

Yields obj and returns the result.

‘my string’.yield_self s s.upcase #=> “MY STRING”


10
11
12
13
# File 'mrbgems/mruby-object-ext/mrblib/object.rb', line 10

def yield_self(&block)
  return to_enum :yield_self unless block
  block.call(self)
end