In Files

  • mrbgems/mruby-enumerator/mrblib/enumerator.rb
  • mrbgems/mruby-io/mrblib/io.rb
  • mrbgems/mruby-io/mrblib/kernel.rb
  • mrbgems/mruby-method/mrblib/kernel.rb
  • mrbgems/mruby-object-ext/mrblib/object.rb
  • mrbgems/mruby-print/mrblib/print.rb
  • mrbgems/mruby-rational/mrblib/rational.rb
  • mrblib/kernel.rb

Kernel

Kernel

ISO 15.3.1

Kernel

ISO 15.3.1

Public Instance Methods

!~(y) click to toggle source

11.4.4 Step c)

 
               # File mrblib/kernel.rb, line 38
def !~(y)
  !(self =~ y)
end
            
Rational(numerator, denominator = 1) click to toggle source
 
               # File mrbgems/mruby-rational/mrblib/rational.rb, line 86
def Rational(numerator, denominator = 1)
  a = numerator
  b = denominator
  a, b = b, a % b until b == 0
  Rational._new(numerator.div(a), denominator.div(a))
end
            
_inspect(_recur_list) click to toggle source

internal method for inspect

 
               # File mrblib/kernel.rb, line 43
def _inspect(_recur_list)
  self.inspect
end
            
`(cmd) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/kernel.rb, line 2
def `(cmd)
  IO.popen(cmd) { |io| io.read }
end
            
enum_for(meth=:each, *args) click to toggle source
Alias for: to_enum
getc(*args) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 389
def getc(*args)
  $stdin.getc(*args)
end
            
gets(*args) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 385
def gets(*args)
  $stdin.gets(*args)
end
            
loop(&block) click to toggle source

Calls the given block repetitively.

ISO 15.3.1.3.29

 
               # 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
            
open(file, *rest, &block) click to toggle source
 
               # 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) click to toggle source

Print human readable object description

ISO 15.3.1.3.34

 
               # File mrbgems/mruby-print/mrblib/print.rb, line 40
def p(*args)
  i = 0
  len = args.size
  while i < len
    __printstr__ args[i].inspect
    __printstr__ "\n"
    i += 1
  end
  args.__svalue
end
            
printf(*args) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 381
def printf(*args)
  $stdout.printf(*args)
end
            
puts(*args) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 377
def puts(*args)
  $stdout.puts(*args)
end
            
singleton_method(name) click to toggle source
 
               # File mrbgems/mruby-method/mrblib/kernel.rb, line 2
def singleton_method(name)
  m = method(name)
  sc = (class <<self; self; end)
  if m.owner != sc
    raise NameError, "undefined method '#{name}' for class '#{sc}'"
  end
  m
end
            
tap{|x|...} → obj click to toggle source

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}"}
 
               # File mrbgems/mruby-object-ext/mrblib/object.rb, line 29
def tap
  yield self
  self
end
            
then(&block) click to toggle source
Alias for: yield_self
to_enum(method = :each, *args) → enum click to toggle source
enum_for(method = :each, *args) → enum

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

Examples

str = "xyz"

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

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

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

Here is such an example with parameter passing:

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

%i[hello world].repeat(2) { |w| puts w }
  # => Prints 'hello', 'hello', 'world', 'world'
enum = (1..14).repeat(3)
  # => returns an Enumerator when called without a block
enum.first(4) # => [1, 1, 1, 2]
 
               # File mrbgems/mruby-enumerator/mrblib/enumerator.rb, line 647
def to_enum(meth=:each, *args)
  Enumerator.new self, meth, *args
end
            
Also aliased as: enum_for
yield_self {|_obj|...} → an_object click to toggle source
then {|_obj|...} → an_object

Yields obj and returns the result.

'my string'.yield_self {|s|s.upcase} #=> "MY STRING"
 
               # 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
            
Also aliased as: then