In Files

  • mrbgems/mruby-enumerator/mrblib/enumerator.rb
  • mrbgems/mruby-io/mrblib/io.rb
  • mrbgems/mruby-io/mrblib/kernel.rb
  • mrbgems/mruby-kernel-ext/mrblib/kernel.rb
  • mrbgems/mruby-method/mrblib/kernel.rb
  • mrbgems/mruby-print/mrblib/print.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
            
_inspect() click to toggle source

internal method for inspect

 
               # File mrblib/kernel.rb, line 43
def _inspect
  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 385
def getc(*args)
  $stdin.getc(*args)
end
            
gets(*args) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 381
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[0]
end
            
printf(*args) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 377
def printf(*args)
  $stdout.printf(*args)
end
            
puts(*args) click to toggle source
 
               # File mrbgems/mruby-io/mrblib/io.rb, line 373
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)
  if m.owner != singleton_class
    raise NameError, "undefined method `#{name}' for class `#{singleton_class}'"
  end
  m
end
            
sprintf(*args) click to toggle source
 
               # File mrbgems/mruby-print/mrblib/print.rb, line 55
def sprintf(*args)
  raise NotImplementedError.new('sprintf not available')
end
            
to_enum(method = :each, *args) → enum click to toggle source
enum_for(method = :each, *args) → enum
to_enum(method = :each, *args) {|*args| block} → enum
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]
 
               # File mrbgems/mruby-enumerator/mrblib/enumerator.rb, line 616
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

Yields obj and returns the result.

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