summaryrefslogtreecommitdiff
path: root/kernel.rb
diff options
context:
space:
mode:
Diffstat (limited to 'kernel.rb')
-rw-r--r--kernel.rb178
1 files changed, 160 insertions, 18 deletions
diff --git a/kernel.rb b/kernel.rb
index de20fffcf9..541d0cfd9d 100644
--- a/kernel.rb
+++ b/kernel.rb
@@ -16,7 +16,7 @@ module Kernel
#++
#
def class
- Primitive.attr! 'inline'
+ Primitive.attr! :leaf
Primitive.cexpr! 'rb_obj_class(self)'
end
@@ -58,14 +58,14 @@ module Kernel
# a.freeze #=> ["a", "b", "c"]
# a.frozen? #=> true
#--
- # Determines if the object is frozen. Equivalent to \c Object\#frozen? in Ruby.
- # \param[in] obj the object to be determines
- # \retval Qtrue if frozen
- # \retval Qfalse if not frozen
+ # Determines if the object is frozen. Equivalent to `Object#frozen?` in Ruby.
+ # @param[in] obj the object to be determines
+ # @retval Qtrue if frozen
+ # @retval Qfalse if not frozen
#++
#
def frozen?
- Primitive.attr! 'inline'
+ Primitive.attr! :leaf
Primitive.cexpr! 'rb_obj_frozen_p(self)'
end
@@ -87,6 +87,7 @@ module Kernel
#++
#
def tap
+ Primitive.attr! :inline_block
yield(self)
self
end
@@ -117,8 +118,18 @@ module Kernel
# # does not meet condition, drop value
# 2.then.detect(&:odd?) # => nil
#
+ # Good usage for +then+ is value piping in method chains:
+ #
+ # require 'open-uri'
+ # require 'json'
+ #
+ # construct_url(arguments).
+ # then {|url| URI(url).read }.
+ # then {|response| JSON.parse(response) }
+ #
def then
- unless Primitive.block_given_p
+ Primitive.attr! :inline_block
+ unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_obj_size)'
end
yield(self)
@@ -132,17 +143,9 @@ module Kernel
#
# "my string".yield_self {|s| s.upcase } #=> "MY STRING"
#
- # Good usage for +then+ is value piping in method chains:
- #
- # require 'open-uri'
- # require 'json'
- #
- # construct_url(arguments).
- # then {|url| URI(url).read }.
- # then {|response| JSON.parse(response) }
- #
def yield_self
- unless Primitive.block_given_p
+ Primitive.attr! :inline_block
+ unless defined?(yield)
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_obj_size)'
end
yield(self)
@@ -150,6 +153,48 @@ module Kernel
module_function
+ # call-seq:
+ # loop { block }
+ # loop -> an_enumerator
+ #
+ # Repeatedly executes the block.
+ #
+ # If no block is given, an enumerator is returned instead.
+ #
+ # loop do
+ # print "Input: "
+ # line = gets
+ # break if !line or line =~ /^q/i
+ # # ...
+ # end
+ #
+ # StopIteration raised in the block breaks the loop. In this case,
+ # loop returns the "result" value stored in the exception.
+ #
+ # enum = Enumerator.new { |y|
+ # y << "one"
+ # y << "two"
+ # :ok
+ # }
+ #
+ # result = loop {
+ # puts enum.next
+ # } #=> :ok
+ def loop
+ Primitive.attr! :inline_block
+ unless defined?(yield)
+ return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_f_loop_size)'
+ end
+
+ begin
+ while true
+ yield
+ end
+ rescue StopIteration => e
+ e.result
+ end
+ end
+
#
# call-seq:
# Float(arg, exception: true) -> float or nil
@@ -169,6 +214,103 @@ module Kernel
# Float("123.0_badstring", exception: false) #=> nil
#
def Float(arg, exception: true)
- Primitive.rb_f_float(arg, exception)
+ if Primitive.mandatory_only?
+ Primitive.rb_f_float1(arg)
+ else
+ Primitive.rb_f_float(arg, exception)
+ end
+ end
+
+ # call-seq:
+ # Integer(object, base = 0, exception: true) -> integer or nil
+ #
+ # Returns an integer converted from +object+.
+ #
+ # Tries to convert +object+ to an integer
+ # using +to_int+ first and +to_i+ second;
+ # see below for exceptions.
+ #
+ # With a non-zero +base+, +object+ must be a string or convertible
+ # to a string.
+ #
+ # ==== numeric objects
+ #
+ # With integer argument +object+ given, returns +object+:
+ #
+ # Integer(1) # => 1
+ # Integer(-1) # => -1
+ #
+ # With floating-point argument +object+ given,
+ # returns +object+ truncated to an integer:
+ #
+ # Integer(1.9) # => 1 # Rounds toward zero.
+ # Integer(-1.9) # => -1 # Rounds toward zero.
+ #
+ # ==== string objects
+ #
+ # With string argument +object+ and zero +base+ given,
+ # returns +object+ converted to an integer in base 10:
+ #
+ # Integer('100') # => 100
+ # Integer('-100') # => -100
+ #
+ # With +base+ zero, string +object+ may contain leading characters
+ # to specify the actual base (radix indicator):
+ #
+ # Integer('0100') # => 64 # Leading '0' specifies base 8.
+ # Integer('0b100') # => 4 # Leading '0b', specifies base 2.
+ # Integer('0x100') # => 256 # Leading '0x' specifies base 16.
+ #
+ # With a positive +base+ (in range 2..36) given, returns +object+
+ # converted to an integer in the given base:
+ #
+ # Integer('100', 2) # => 4
+ # Integer('100', 8) # => 64
+ # Integer('-100', 16) # => -256
+ #
+ # With a negative +base+ (in range -36..-2) given, returns +object+
+ # converted to an integer in the radix indicator if exists or
+ # +-base+:
+ #
+ # Integer('0x100', -2) # => 256
+ # Integer('100', -2) # => 4
+ # Integer('0b100', -8) # => 4
+ # Integer('100', -8) # => 64
+ # Integer('0o100', -10) # => 64
+ # Integer('100', -10) # => 100
+ #
+ # +base+ -1 is equal the -10 case.
+ #
+ # When converting strings, surrounding whitespace and embedded underscores
+ # are allowed and ignored:
+ #
+ # Integer(' 100 ') # => 100
+ # Integer('-1_0_0', 16) # => -256
+ #
+ # ==== other classes
+ #
+ # Examples with +object+ of various other classes:
+ #
+ # Integer(Rational(9, 10)) # => 0 # Rounds toward zero.
+ # Integer(Complex(2, 0)) # => 2 # Imaginary part must be zero.
+ # Integer(Time.now) # => 1650974042
+ #
+ # ==== keywords
+ #
+ # With optional keyword argument +exception+ given as +true+ (the default):
+ #
+ # - Raises TypeError if +object+ does not respond to +to_int+ or +to_i+.
+ # - Raises TypeError if +object+ is +nil+.
+ # - Raise ArgumentError if +object+ is an invalid string.
+ #
+ # With +exception+ given as +false+, an exception of any kind is suppressed
+ # and +nil+ is returned.
+
+ def Integer(arg, base = 0, exception: true)
+ if Primitive.mandatory_only?
+ Primitive.rb_f_integer1(arg)
+ else
+ Primitive.rb_f_integer(arg, base, exception);
+ end
end
end