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
-
.Array(arg) ⇒ Array
Returns +arg+ as an Array.
-
.caller ⇒ Object
-
.fail ⇒ Object
With no arguments, raises a
RuntimeErrorWith a single +String+ argument, raises a +RuntimeError+ with the string as a message. -
.Float(arg) ⇒ Float
Returns arg converted to a float.
-
.Hash(arg) ⇒ Hash
Converts arg to a
Hashby calling arg.to_hash. -
.Integer(arg, base = 0) ⇒ Integer
Converts arg to a
Fixnum. -
.itself ⇒ Object
Returns obj.
-
.String(arg) ⇒ String
Returns arg as an
String.
Instance Method Summary collapse
-
#!~(y) ⇒ Object
11.4.4 Step c).
-
#===(other) ⇒ Boolean
Case Equality—For class
Object, effectively the same as calling#==, but typically overridden by descendants to provide meaningful semantics incasestatements. -
#__case_eqq ⇒ Object
internal.
-
#__method__ ⇒ Object
Returns the name at the definition of the current method as a Symbol.
-
#__printstr__ ⇒ Object
15.3.1.3.34.
-
#_inspect ⇒ Object
internal method for inspect.
-
#`(cmd) ⇒ Object
15.3.1.2.1 Kernel.
provided by Kernel#15.3.1.3.5. -
#block_given? ⇒ Boolean
Returns
trueifyieldwould execute a block in the current context. -
#class ⇒ Class
Returns the class of obj.
-
#class_defined? ⇒ Boolean
not reached.
-
#clone ⇒ Object
Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference.
-
#define_singleton_method ⇒ Object
15.3.1.3.45.
-
#dup ⇒ Object
Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference.
-
#eql? ⇒ Boolean
Equality—At the
Objectlevel,==returnstrueonly if obj and other are the same object. -
#equal? ⇒ Boolean
Equality—At the
Objectlevel,==returnstrueonly if obj and other are the same object. -
#extend ⇒ Object
Adds to obj the instance methods from each module given as a parameter.
-
#format ⇒ Object
in sprintf.c.
-
#freeze ⇒ Object
15.3.1.3.13.
-
#frozen? ⇒ Boolean
-
#getc(*args) ⇒ Object
-
#gets(*args) ⇒ Object
-
#global_variables ⇒ Object
15.3.1.3.14.
-
#hash ⇒ Fixnum
Generates a
Fixnumhash value for this object. -
#initialize_copy ⇒ Object
15.3.1.3.16.
-
#inspect ⇒ String
Returns a string containing a human-readable representation of obj.
-
#instance_of? ⇒ Boolean
Returns
trueif obj is an instance of the given class. -
#instance_variable_defined?(symbol) ⇒ Boolean
Returns
trueif the given instance variable is defined in obj. -
#instance_variable_get(symbol) ⇒ Object
Returns the value of the given instance variable, or nil if the instance variable is not set.
-
#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.
-
#instance_variables ⇒ Object
15.3.1.3.23.
-
#is_a? ⇒ Boolean
Returns
trueif class is the class of obj, or if class is one of the superclasses of obj or modules included in obj. -
#iterator? ⇒ Boolean
Returns
trueifyieldwould execute a block in the current context. -
#kind_of? ⇒ Boolean
Returns
trueif class is the class of obj, or if class is one of the superclasses of obj or modules included in obj. -
#local_variables ⇒ Array
Returns the names of local variables in the current scope.
-
#loop(&block) ⇒ Object
Calls the given block repetitively.
-
#method_missing ⇒ Object
15.3.1.3.30.
-
#methods ⇒ Array
Returns a list of the names of methods publicly accessible in obj.
-
#nil? ⇒ Boolean
call_seq: nil.nil? -> true
.nil? -> false. -
#object_id ⇒ Object
call-seq: obj.id -> fixnum obj.object_id -> fixnum.
-
#open(file, *rest, &block) ⇒ Object
-
#p(*args) ⇒ Object
Print human readable object description.
-
#print(*args) ⇒ Object
Invoke method +print+ on STDOUT and passing +*args+.
-
#printf(*args) ⇒ Object
-
#private_methods(all = true) ⇒ Array
Returns the list of private methods accessible to obj.
-
#protected_methods(all = true) ⇒ Array
Returns the list of protected methods accessible to obj.
-
#public_methods(all = true) ⇒ Array
Returns the list of public methods accessible to obj.
-
#puts(*args) ⇒ Object
Invoke method +puts+ on STDOUT and passing +args+.
-
#raise ⇒ Object
With no arguments, raises a
RuntimeErrorWith a single +String+ argument, raises a +RuntimeError+ with the string as a message. -
#remove_instance_variable(symbol) ⇒ Object
Removes the named instance variable from obj, returning that variable’s value.
-
#respond_to?(symbol, include_private = false) ⇒ Boolean
Returns +true+ if obj responds to the given method.
-
#send ⇒ Object
Invokes the method identified by symbol, passing it any arguments specified.
-
#singleton_class ⇒ Object
15.3.1.2.12.
-
#singleton_method(name) ⇒ Object
-
#singleton_methods(all = true) ⇒ Array
Returns an array of the names of singleton methods for obj.
-
#sprintf(*args) ⇒ Object
in sprintf.c.
-
#to_enum(meth = :each, *args) ⇒ Object
(also: #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. -
#to_s ⇒ Object
15.3.1.3.46.
-
#yield_self(&block) ⇒ Object
call-seq: obj.yield_self _obj … -> an_object.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Object
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]
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; } |
.caller ⇒ Object
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)); } |
.raise ⇒ Object .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
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
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
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); } |
.itself ⇒ Object
Returns obj.
string = 'my string' #=> "my string"
string.itself.object_id == string.object_id #=> true
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”
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.
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_eqq ⇒ Object
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; } |
#_inspect ⇒ Object
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”
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(); } |
#class ⇒ Class
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
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
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))); } |
#clone ⇒ Object
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 #=> #
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.
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_method ⇒ Object
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); } |
#dup ⇒ Object
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.
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
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
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)); } |
#extend ⇒ Object
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) #=> #
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); } |
#format ⇒ 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);
|
#freeze ⇒ Object
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
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_variables ⇒ Object
15.3.1.3.14
#hash ⇒ Fixnum
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.
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_copy ⇒ Object
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; } |
#inspect ⇒ String
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”
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?.
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
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
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">”
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_variables ⇒ Object
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
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”
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
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_variables ⇒ Array
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.
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 |
#methods ⇒ Array
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
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
true to nil?.
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_id ⇒ Object
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
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 |
#print(*args) ⇒ Object
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.
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.
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.
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 |
#raise ⇒ Object #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
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.
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”
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_class ⇒ Object
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]
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_s ⇒ Object
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 |