Module: Kernel

Defined in:
mrblib/kernel.rb,
src/kernel.c,
mrbgems/mruby-sprintf/src/kernel.c,
mrbgems/mruby-io/mrblib/io.rb,
mrbgems/mruby-io/mrblib/kernel.rb,
mrbgems/mruby-print/mrblib/print.rb,
mrbgems/mruby-method/mrblib/kernel.rb,
mrbgems/mruby-kernel-ext/mrblib/kernel.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


996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'src/kernel.c', line 996

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.

First tries to call Array#to_ary on +arg+, then Array#to_a.

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

Returns:


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'mrbgems/mruby-kernel-ext/src/kernel.c', line 176

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_ary");
  if (mrb_nil_p(tmp)) {
    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
62
# 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_type(v) == MRB_TT_RANGE) {
        mrb_int beg, len;
        if (mrb_range_beg_len(mrb, v, &beg, &len, bt_len, TRUE) == 1) {
          lev = beg;
          n = len;
        }
        else {
          return mrb_nil_value();
        }
      }
      else {
        v = mrb_to_int(mrb, v);
        lev = mrb_fixnum(v);
        if (lev < 0) {
          mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative level (%S)", v);
        }
        n = bt_len - lev;
      }
      break;
    case 2:
      lev = mrb_fixnum(mrb_to_int(mrb, v));
      n = mrb_fixnum(mrb_to_int(mrb, length));
      if (lev < 0) {
        mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative level (%S)", v);
      }
      if (n < 0) {
        mrb_raisef(mrb, E_ARGUMENT_ERROR, "negative size (%S)", 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


882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
# File 'src/kernel.c', line 882

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:


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

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

Converts arg to a Hash by calling arg.to_hash. Returns an empty Hash when arg is nil or [].

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

Returns:


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'mrbgems/mruby-kernel-ext/src/kernel.c', line 207

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

  mrb_get_args(mrb, "o", &arg);
  if (mrb_nil_p(arg)) {
    return mrb_hash_new(mrb);
  }
  tmp = mrb_check_convert_type(mrb, arg, MRB_TT_HASH, "Hash", "to_hash");
  if (mrb_nil_p(tmp)) {
    if (mrb_array_p(arg) && RARRAY_LEN(arg) == 0) {
      return mrb_hash_new(mrb);
    }
    mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S into Hash",
      mrb_str_new_cstr(mrb, mrb_obj_classname(mrb, arg)));
  }
  return tmp;
}

.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 converted using to_int, and to_i. 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:


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

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);
}

.itselfObject

Returns obj.

string = 'my string' #=> "my string"
string.itself.object_id == string.object_id #=> true

Returns:


237
238
239
240
241
# File 'mrbgems/mruby-kernel-ext/src/kernel.c', line 237

static mrb_value
mrb_f_itself(mrb_state *mrb, mrb_value self)
{
  return self;
}

.String(arg) ⇒ String

Returns arg as an String.

First tries to call its to_str method, then its to_s method.

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

Returns:


152
153
154
155
156
157
158
159
160
161
162
163
# File 'mrbgems/mruby-kernel-ext/src/kernel.c', line 152

static mrb_value
mrb_f_string(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_STRING, "String", "to_str");
  if (mrb_nil_p(tmp)) {
    tmp = mrb_check_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)

84
85
86
87
88
89
90
91
# File 'src/kernel.c', line 84

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


1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
# File 'src/kernel.c', line 1142

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.


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

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


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

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

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

  return argv;
}

#_inspectObject

internal method for inspect


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

def _inspect
  self.inspect
end

#`(cmd) ⇒ Object

15.3.1.2.1 Kernel. provided by Kernel# 15.3.1.3.5


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)

Returns:

  • (Boolean)

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
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'src/kernel.c', line 140

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)

207
208
209
210
211
# File 'src/kernel.c', line 207

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

#class_defined?Boolean

not reached

Returns:

  • (Boolean)

909
910
911
912
913
914
915
916
# File 'src/kernel.c', line 909

static mrb_value
mrb_krn_class_defined(mrb_state *mrb, mrb_value self)
{
  mrb_value str;

  mrb_get_args(mrb, "S", &str);
  return mrb_bool_value(mrb_class_defined(mrb, RSTRING_PTR(str)));
}

#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:


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'src/kernel.c', line 329

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 %S", self);
  }
  if (mrb_type(self) == MRB_TT_SCLASS) {
    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);

  return clone;
}

#define_singleton_methodObject

15.3.1.3.45


1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
# File 'src/kernel.c', line 1122

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);
  if (mrb_nil_p(blk)) {
    mrb_raise(mrb, E_ARGUMENT_ERROR, "no block given");
  }
  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:


369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'src/kernel.c', line 369

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 %S", obj);
  }
  if (mrb_type(obj) == MRB_TT_SCLASS) {
    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)

Returns:

  • (Boolean)

1705
1706
1707
1708
1709
1710
1711
1712
# File 'src/class.c', line 1705

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));
}

#==(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)

Returns:

  • (Boolean)

1705
1706
1707
1708
1709
1710
1711
1712
# File 'src/class.c', line 1705

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:


431
432
433
434
435
436
437
438
439
# File 'src/kernel.c', line 431

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


441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'src/kernel.c', line 441

static mrb_value
mrb_obj_freeze(mrb_state *mrb, mrb_value self)
{
  struct RBasic *b;

  switch (mrb_type(self)) {
    case MRB_TT_FALSE:
    case MRB_TT_TRUE:
    case MRB_TT_FIXNUM:
    case MRB_TT_SYMBOL:
#ifndef MRB_WITHOUT_FLOAT
    case MRB_TT_FLOAT:
#endif
      return self;
    default:
      break;
  }

  b = mrb_basic_ptr(self);
  if (!MRB_FROZEN_P(b)) {
    MRB_SET_FROZEN_FLAG(b);
  }
  return self;
}

#frozen?Boolean

Returns:

  • (Boolean)

466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'src/kernel.c', line 466

static mrb_value
mrb_obj_frozen(mrb_state *mrb, mrb_value self)
{
  struct RBasic *b;

  switch (mrb_type(self)) {
    case MRB_TT_FALSE:
    case MRB_TT_TRUE:
    case MRB_TT_FIXNUM:
    case MRB_TT_SYMBOL:
#ifndef MRB_WITHOUT_FLOAT
    case MRB_TT_FLOAT:
#endif
      return mrb_true_value();
    default:
      break;
  }

  b = mrb_basic_ptr(self);
  if (!MRB_FROZEN_P(b)) {
    return mrb_false_value();
  }
  return mrb_true_value();
}

#getc(*args) ⇒ Object


385
386
387
# File 'mrbgems/mruby-io/mrblib/io.rb', line 385

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

#gets(*args) ⇒ Object


381
382
383
# File 'mrbgems/mruby-io/mrblib/io.rb', line 381

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

#global_variablesObject

15.3.1.3.14

#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:


502
503
504
505
506
# File 'src/kernel.c', line 502

MRB_API 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


509
510
511
512
513
514
515
516
517
518
519
520
# File 'src/kernel.c', line 509

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:


66
67
68
69
70
71
72
73
# File 'src/kernel.c', line 66

MRB_API mrb_value
mrb_obj_inspect(mrb_state *mrb, mrb_value obj)
{
  if ((mrb_type(obj) == MRB_TT_OBJECT) && 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)

Returns:

  • (Boolean)

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

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)

Returns:

  • (Boolean)

566
567
568
569
570
571
572
573
574
# File 'src/kernel.c', line 566

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

  mrb_get_args(mrb, "n", &sym);
  mrb_iv_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:


596
597
598
599
600
601
602
603
604
# File 'src/kernel.c', line 596

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_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:


626
627
628
629
630
631
632
633
634
635
636
# File 'src/kernel.c', line 626

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_check(mrb, iv_name);
  mrb_iv_set(mrb, self, iv_name, val);
  return val;
}

#instance_variablesObject

15.3.1.3.23

#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)

Returns:

  • (Boolean)

665
666
667
668
669
670
671
672
673
# File 'src/kernel.c', line 665

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)

Returns:

  • (Boolean)

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
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'src/kernel.c', line 140

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)

Returns:

  • (Boolean)

665
666
667
668
669
670
671
672
673
# File 'src/kernel.c', line 665

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:


1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
# File 'src/kernel.c', line 1171

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_hash_set(mrb, vars, mrb_symbol_value(irep->lv[i].name), mrb_true_value());
      }
    }
    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:


788
789
790
791
792
793
794
# File 'src/kernel.c', line 788

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)

804
805
806
807
808
# File 'src/kernel.c', line 804

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.


110
111
112
113
114
# File 'src/kernel.c', line 110

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[0]
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


377
378
379
# File 'mrbgems/mruby-io/mrblib/io.rb', line 377

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:


819
820
821
822
823
824
825
# File 'src/kernel.c', line 819

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:


836
837
838
839
840
841
842
# File 'src/kernel.c', line 836

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:


853
854
855
856
857
858
859
# File 'src/kernel.c', line 853

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


882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
# File 'src/kernel.c', line 882

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 */
}

#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:


941
942
943
944
945
946
947
948
949
950
951
952
953
954
# File 'src/kernel.c', line 941

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_check(mrb, sym);
  val = mrb_iv_remove(mrb, self, sym);
  if (mrb_undef_p(val)) {
    mrb_name_error(mrb, sym, "instance variable %S not defined", mrb_sym2str(mrb, 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)

Returns:

  • (Boolean)

1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
# File 'src/kernel.c', line 1031

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

  mrb_get_args(mrb, "o|b", &mid, &priv);

  if (mrb_symbol_p(mid)) {
    id = mrb_symbol(mid);
  }
  else {
    mrb_value tmp;
    if (mrb_string_p(mid)) {
      tmp = mrb_check_intern_str(mrb, mid);
    }
    else {
      tmp = mrb_check_string_type(mrb, mid);
      if (mrb_nil_p(tmp)) {
        tmp = mrb_inspect(mrb, mid);
        mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a symbol", tmp);
      }
      tmp = mrb_check_intern_str(mrb, tmp);
    }
    if (mrb_nil_p(tmp)) {
      respond_to_p = FALSE;
    }
    else {
      id = mrb_symbol(tmp);
    }
  }

  if (respond_to_p) {
    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] = mid;
      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:


569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
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
611
612
613
# File 'src/vm.c', line 569

MRB_API 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.2.12


1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
# File 'src/class.c', line 1301

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
# File 'mrbgems/mruby-method/mrblib/kernel.rb', line 2

def singleton_method(name)
  m = method(name)
  if m.owner != singleton_class
    raise NameError, "undefined method `#{name}' for class `#{singleton_class}'"
  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:


1114
1115
1116
1117
1118
1119
1120
# File 'src/kernel.c', line 1114

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);
}

#sprintf(*args) ⇒ Object

in sprintf.c


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

mrb_value mrb_f_sprintf(mrb_state *mrb, mrb_value obj);

#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 obj.to_enum(method = :each, *args) {|args| block} -> enum obj.enum_for(method = :each, args){|args| block} -> enum

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

If a block is given, it will be used to calculate the size of the enumerator without the need to iterate it (see Enumerator#size).

=== 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 and a sizing block:

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) do # __method__ is :repeat here
        sz = size     # Call size and multiply by n...
        sz * n if sz  # but return nil if size itself is nil
      end
    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]

616
617
618
# File 'mrbgems/mruby-enumerator/mrblib/enumerator.rb', line 616

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

#to_sObject

15.3.1.3.46

#yield_self(&block) ⇒ Object

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

Yields obj and returns the result.

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

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

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