Module: Kernel
- Defined in:
- mrblib/kernel.rb,
mrblib/00kernel.rb,
mrbgems/mruby-io/mrblib/kernel.rb,
mrbgems/mruby-object-ext/mrblib/object.rb,
mrbgems/mruby-enumerator/mrblib/enumerator.rb
Overview
Kernel
ISO 15.3.1
Instance Method Summary collapse
-
#!~(y) ⇒ Object
11.4.4 Step c).
-
#extend(*args) ⇒ Object
call-seq: obj.extend(module, ...) -> obj.
-
#tap {|_self| ... } ⇒ Object
call-seq: obj.tap{|x|...} -> obj.
-
#to_enum(meth = :each, *args, **kwd) ⇒ Object
(also: #enum_for)
call-seq: obj.to_enum(method = :each, *args) -> enum obj.enum_for(method = :each, *args) -> enum.
-
#yield_self(&block) ⇒ Object
(also: #then)
call-seq: obj.yield_self {|_obj|...} -> an_object obj.then {|_obj|...} -> an_object.
Instance Method Details
#!~(y) ⇒ Object
11.4.4 Step c)
38 39 40 |
# File 'mrblib/kernel.rb', line 38 def !~(y) !(self =~ y) end |
#extend(*args) ⇒ Object
call-seq: obj.extend(module, ...) -> obj
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) #=> #<Klass:0x401b3bc8>
k.hello #=> "Hello from Mod.\n"
ISO 15.3.1.3.13
27 28 29 30 31 32 33 34 35 |
# File 'mrblib/00kernel.rb', line 27 def extend(*args) args.reverse! obj = self args.each do |m| m.__send__(:extend_object, obj) m.__send__(:extended, obj) end self end |
#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}"}
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, **kwd) ⇒ 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(__callee__, n) do # __callee__ 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]
662 663 664 |
# File 'mrbgems/mruby-enumerator/mrblib/enumerator.rb', line 662 def to_enum(*a) raise NotImplementedError.new("fiber required for enumerator") end |
#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 |