11.4.4 Step c)
# File mrblib/kernel.rb, line 38 def !~(y) !(self =~ y) end
# 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
internal method for inspect
# File mrblib/kernel.rb, line 43 def _inspect(_recur_list) self.inspect end
# File mrbgems/mruby-io/mrblib/kernel.rb, line 2 def `(cmd) IO.popen(cmd) { |io| io.read } end
# File mrbgems/mruby-io/mrblib/io.rb, line 389 def getc(*args) $stdin.getc(*args) end
# File mrbgems/mruby-io/mrblib/io.rb, line 385 def gets(*args) $stdin.gets(*args) end
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
# 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
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
# File mrbgems/mruby-io/mrblib/io.rb, line 373 def print(*args) $stdout.print(*args) end
# File mrbgems/mruby-io/mrblib/io.rb, line 381 def printf(*args) $stdout.printf(*args) end
# File mrbgems/mruby-io/mrblib/io.rb, line 377 def puts(*args) $stdout.puts(*args) end
# 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
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
Creates a new Enumerator which will enumerate
by calling method
on obj
, passing
args
if any.
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
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