Module: Kernel
- Defined in:
- ext/enterprise_script_service/mruby/mrblib/kernel.rb,
ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.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-kernel-ext/mrblib/kernel.rb,
ext/enterprise_script_service/mruby/mrbgems/mruby-enumerator/mrblib/enumerator.rb
Overview
Kernel
ISO 15.3.1
Instance Method Summary collapse
-
#!~(y) ⇒ Object
11.4.4 Step c).
-
#_inspect ⇒ Object
internal method for inspect.
-
#`(cmd) ⇒ Object
15.3.1.2.1 Kernel.‘ provided by Kernel#` 15.3.1.3.5.
- #getc(*args) ⇒ Object
- #gets(*args) ⇒ Object
-
#loop(&block) ⇒ Object
Calls the given block repetitively.
- #open(file, *rest, &block) ⇒ Object
-
#p(*args) ⇒ Object
Print human readable object description.
-
#print(*args) ⇒ Object
Invoke method
printon STDOUT and passing *args. - #printf(*args) ⇒ Object
-
#puts(*args) ⇒ Object
Invoke method
putson STDOUT and passing args. - #singleton_method(name) ⇒ Object
- #sprintf(*args) ⇒ Object
-
#to_enum(meth = :each, *args) ⇒ Object
(also: #enum_for)
call-seq: obj.to_enum(method = :each, *args) -> enum obj.enum_for(method = :each, *args) -> enum obj.to_enum(method = :each, *args) {|*args| block} -> enum obj.enum_for(method = :each, *args){|*args| block} -> enum.
-
#yield_self(&block) ⇒ Object
call-seq: obj.yield_self {|_obj|…} -> an_object.
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 ⇒ Object
internal method for inspect
43 44 45 |
# File 'ext/enterprise_script_service/mruby/mrblib/kernel.rb', line 43 def _inspect self.inspect end |
#`(cmd) ⇒ Object
15.3.1.2.1 Kernel.‘ provided by Kernel#` 15.3.1.3.5
10 11 12 |
# File 'ext/enterprise_script_service/mruby/mrblib/kernel.rb', line 10 def `(s) raise NotImplementedError.new("backquotes not implemented") end |
#getc(*args) ⇒ Object
384 385 386 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 384 def getc(*args) $stdin.getc(*args) end |
#gets(*args) ⇒ Object
380 381 382 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 380 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
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[0] end |
#print(*args) ⇒ Object
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
376 377 378 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-io/mrblib/io.rb', line 376 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 |
#singleton_method(name) ⇒ Object
2 3 4 5 6 7 8 |
# File 'ext/enterprise_script_service/mruby/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) ⇒ Object
55 56 57 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-print/mrblib/print.rb', line 55 def sprintf(*args) raise NotImplementedError.new('sprintf not available') 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
obj.to_enum(method = :each, *args) {|*args| block} -> enum
obj.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]
616 617 618 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-enumerator/mrblib/enumerator.rb', line 616 def to_enum(*a) raise NotImplementedError.new("fiber required for enumerator") end |
#yield_self(&block) ⇒ Object
call-seq:
obj.yield_self {|_obj|...} -> an_object
Yields obj and returns the result.
'my string'.yield_self {|s|s.upcase} #=> "MY STRING"
9 10 11 12 |
# File 'ext/enterprise_script_service/mruby/mrbgems/mruby-kernel-ext/mrblib/kernel.rb', line 9 def yield_self(&block) return to_enum :yield_self unless block block.call(self) end |