Module: Kernel

Defined in:
ext/enterprise_script_service/mruby/mrblib/kernel.rb,
ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/kernel.rb,
ext/enterprise_script_service/mruby/mrbgems/mruby-print/mrblib/print.rb,
ext/enterprise_script_service/mruby/mrbgems/mruby-method/mrblib/kernel.rb,
ext/enterprise_script_service/mruby/mrbgems/mruby-object-ext/mrblib/object.rb,
ext/enterprise_script_service/mruby/mrbgems/mruby-rational/mrblib/rational.rb,
ext/enterprise_script_service/mruby/mrbgems/mruby-enumerator/mrblib/enumerator.rb

Overview

Kernel

ISO 15.3.1

Instance Method Summary collapse

Instance Method Details

#!~(y) ⇒ Object

11.4.4 Step c)



38
39
40
# File 'ext/enterprise_script_service/mruby/mrblib/kernel.rb', line 38

def !~(y)
  !(self =~ y)
end

#_inspect(_recur_list) ⇒ Object

internal method for inspect



43
44
45
# File 'ext/enterprise_script_service/mruby/mrblib/kernel.rb', line 43

def _inspect(_recur_list)
  self.inspect
end

#`(cmd) ⇒ Object

15.3.1.2.1 Kernel.‘ provided by Kernel#` 15.3.1.3.3



10
11
12
# File 'ext/enterprise_script_service/mruby/mrblib/kernel.rb', line 10

def `(s)
  raise NotImplementedError.new("backquotes not implemented")
end

#gets(*args) ⇒ Object



28
29
30
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/kernel.rb', line 28

def gets(*args)
  $stdin.gets(*args)
end

#loop(&block) ⇒ Object

Calls the given block repetitively.

ISO 15.3.1.3.29



27
28
29
30
31
32
33
34
35
# File 'ext/enterprise_script_service/mruby/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) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
# File 'ext/enterprise_script_service/mruby/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 'ext/enterprise_script_service/mruby/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

Invoke method print on STDOUT and passing *args

ISO 15.3.1.2.10



10
11
12
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-print/mrblib/print.rb', line 10

def print(*args)
  $stdout.print(*args)
end

#printf(*args) ⇒ Object



24
25
26
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/kernel.rb', line 24

def printf(*args)
  $stdout.printf(*args)
end

#puts(*args) ⇒ Object

Invoke method puts on STDOUT and passing args

ISO 15.3.1.2.11



23
24
25
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-print/mrblib/print.rb', line 23

def puts(*args)
  $stdout.puts(*args)
end

#Rational(numerator, denominator = 1) ⇒ Object



86
87
88
89
90
91
# File 'ext/enterprise_script_service/mruby/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

#singleton_method(name) ⇒ Object



2
3
4
5
6
7
8
9
# File 'ext/enterprise_script_service/mruby/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 {|_self| ... } ⇒ Object

call-seq:

   obj.tap{|x|...}    -> obj

Yields <code>x</code> to the block, and then returns <code>x</code>.
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}"}

Yields:

  • (_self)

Yield Parameters:

  • _self (Kernel)

    the object that the method was called on



29
30
31
32
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-object-ext/mrblib/object.rb', line 29

def tap
  yield self
  self
end

#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

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]


647
648
649
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-enumerator/mrblib/enumerator.rb', line 647

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 'ext/enterprise_script_service/mruby/mrbgems/mruby-object-ext/mrblib/object.rb', line 10

def yield_self(&block)
  return to_enum :yield_self unless block
  block.call(self)
end