In Files

  • mrbgems/mruby-enumerator/mrblib/enumerator.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
            
`(s) click to toggle source

15.3.1.2.1 Kernel.` provided by Kernel#` 15.3.1.3.5

 
               # File mrblib/kernel.rb, line 10
def `(s)
  raise NotImplementedError.new("backquotes not implemented")
end
            
enum_for(meth=:each, *args) click to toggle source
Alias for: to_enum
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
  nil
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-print/mrblib/print.rb, line 52
def printf(*args)
  raise NotImplementedError.new('printf not available')
end
            
puts(*args) click to toggle source

Invoke method puts on STDOUT and passing +args+

ISO 15.3.1.2.11

 
               # File mrbgems/mruby-print/mrblib/print.rb, line 23
def puts(*args)
  i = 0
  len = args.size
  while i < len
    s = args[i].to_s
    __printstr__ s
    __printstr__ "\n" if (s[-1] != "\n")
    i += 1
  end
  __printstr__ "\n" if len == 0
  nil
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 607
def to_enum(meth=:each, *args)
  Enumerator.new self, meth, *args
end
            
Also aliased as: enum_for