6 # Returns the class of <i>obj</i>. This method must always be called
7 # with an explicit receiver, as #class is also a reserved word in
11 # self.class #=> Object
13 # Equivalent to \c Object\#class in Ruby.
15 # Returns the class of \c obj, skipping singleton classes or module inclusions.
20 Primitive.cexpr! 'rb_obj_class(self)'
25 # obj.clone(freeze: nil) -> an_object
27 # Produces a shallow copy of <i>obj</i>---the instance variables of
28 # <i>obj</i> are copied, but not the objects they reference.
29 # #clone copies the frozen value state of <i>obj</i>, unless the
30 # +:freeze+ keyword argument is given with a false or true value.
31 # See also the discussion under Object#dup.
36 # s1 = Klass.new #=> #<Klass:0x401b3a38>
37 # s1.str = "Hello" #=> "Hello"
38 # s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello">
39 # s2.str[1,4] = "i" #=> "i"
40 # s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">"
41 # s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
43 # This method may have class-specific behavior. If so, that
44 # behavior will be documented under the #+initialize_copy+ method of
47 def clone(freeze: nil)
48 Primitive.rb_obj_clone2(freeze)
53 # obj.frozen? -> true or false
55 # Returns the freeze status of <i>obj</i>.
57 # a = [ "a", "b", "c" ]
58 # a.freeze #=> ["a", "b", "c"]
61 # Determines if the object is frozen. Equivalent to `Object#frozen?` in Ruby.
62 # @param[in] obj the object to be determines
63 # @retval Qtrue if frozen
64 # @retval Qfalse if not frozen
69 Primitive.cexpr! 'rb_obj_frozen_p(self)'
74 # obj.tap {|x| block } -> obj
76 # Yields self to the block, and then returns self.
77 # The primary purpose of this method is to "tap into" a method chain,
78 # in order to perform operations on intermediate results within the chain.
80 # (1..10) .tap {|x| puts "original: #{x}" }
81 # .to_a .tap {|x| puts "array: #{x}" }
82 # .select {|x| x.even? } .tap {|x| puts "evens: #{x}" }
83 # .map {|x| x*x } .tap {|x| puts "squares: #{x}" }
90 Primitive.attr! :inline_block
97 # obj.then {|x| block } -> an_object
99 # Yields self to the block and returns the result of the block.
101 # 3.next.then {|x| x**x }.to_s #=> "256"
103 # Good usage for +then+ is value piping in method chains:
108 # construct_url(arguments).
109 # then {|url| URI(url).read }.
110 # then {|response| JSON.parse(response) }
112 # When called without block, the method returns +Enumerator+,
113 # which can be used, for example, for conditional
116 # # meets condition, no-op
117 # 1.then.detect(&:odd?) # => 1
118 # # does not meet condition, drop value
119 # 2.then.detect(&:odd?) # => nil
121 # Good usage for +then+ is value piping in method chains:
126 # construct_url(arguments).
127 # then {|url| URI(url).read }.
128 # then {|response| JSON.parse(response) }
131 Primitive.attr! :inline_block
132 unless defined?(yield)
133 return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_obj_size)'
140 # obj.yield_self {|x| block } -> an_object
142 # Yields self to the block and returns the result of the block.
144 # "my string".yield_self {|s| s.upcase } #=> "MY STRING"
147 Primitive.attr! :inline_block
148 unless defined?(yield)
149 return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_obj_size)'
158 # loop -> an_enumerator
160 # Repeatedly executes the block.
162 # If no block is given, an enumerator is returned instead.
167 # break if !line or line =~ /^q/i
171 # StopIteration raised in the block breaks the loop. In this case,
172 # loop returns the "result" value stored in the exception.
174 # enum = Enumerator.new { |y|
184 Primitive.attr! :inline_block
185 unless defined?(yield)
186 return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_f_loop_size)'
193 rescue StopIteration => e
200 # Float(arg, exception: true) -> float or nil
202 # Returns <i>arg</i> converted to a float. Numeric types are
203 # converted directly, and with exception to String and
204 # <code>nil</code> the rest are converted using
205 # <i>arg</i><code>.to_f</code>. Converting a String with invalid
206 # characters will result in a ArgumentError. Converting
207 # <code>nil</code> generates a TypeError. Exceptions can be
208 # suppressed by passing <code>exception: false</code>.
211 # Float("123.456") #=> 123.456
212 # Float("123.0_badstring") #=> ArgumentError: invalid value for Float(): "123.0_badstring"
213 # Float(nil) #=> TypeError: can't convert nil into Float
214 # Float("123.0_badstring", exception: false) #=> nil
216 def Float(arg, exception: true)
217 if Primitive.mandatory_only?
218 Primitive.rb_f_float1(arg)
220 Primitive.rb_f_float(arg, exception)
225 # Integer(object, base = 0, exception: true) -> integer or nil
227 # Returns an integer converted from +object+.
229 # Tries to convert +object+ to an integer
230 # using +to_int+ first and +to_i+ second;
231 # see below for exceptions.
233 # With a non-zero +base+, +object+ must be a string or convertible
236 # ==== numeric objects
238 # With integer argument +object+ given, returns +object+:
241 # Integer(-1) # => -1
243 # With floating-point argument +object+ given,
244 # returns +object+ truncated to an integer:
246 # Integer(1.9) # => 1 # Rounds toward zero.
247 # Integer(-1.9) # => -1 # Rounds toward zero.
249 # ==== string objects
251 # With string argument +object+ and zero +base+ given,
252 # returns +object+ converted to an integer in base 10:
254 # Integer('100') # => 100
255 # Integer('-100') # => -100
257 # With +base+ zero, string +object+ may contain leading characters
258 # to specify the actual base (radix indicator):
260 # Integer('0100') # => 64 # Leading '0' specifies base 8.
261 # Integer('0b100') # => 4 # Leading '0b', specifies base 2.
262 # Integer('0x100') # => 256 # Leading '0x' specifies base 16.
264 # With a positive +base+ (in range 2..36) given, returns +object+
265 # converted to an integer in the given base:
267 # Integer('100', 2) # => 4
268 # Integer('100', 8) # => 64
269 # Integer('-100', 16) # => -256
271 # With a negative +base+ (in range -36..-2) given, returns +object+
272 # converted to an integer in the radix indicator if exists or
275 # Integer('0x100', -2) # => 256
276 # Integer('100', -2) # => 4
277 # Integer('0b100', -8) # => 4
278 # Integer('100', -8) # => 64
279 # Integer('0o100', -10) # => 64
280 # Integer('100', -10) # => 100
282 # +base+ -1 is equal the -10 case.
284 # When converting strings, surrounding whitespace and embedded underscores
285 # are allowed and ignored:
287 # Integer(' 100 ') # => 100
288 # Integer('-1_0_0', 16) # => -256
292 # Examples with +object+ of various other classes:
294 # Integer(Rational(9, 10)) # => 0 # Rounds toward zero.
295 # Integer(Complex(2, 0)) # => 2 # Imaginary part must be zero.
296 # Integer(Time.now) # => 1650974042
300 # With optional keyword argument +exception+ given as +true+ (the default):
302 # - Raises TypeError if +object+ does not respond to +to_int+ or +to_i+.
303 # - Raises TypeError if +object+ is +nil+.
304 # - Raise ArgumentError if +object+ is an invalid string.
306 # With +exception+ given as +false+, an exception of any kind is suppressed
307 # and +nil+ is returned.
309 def Integer(arg, base = 0, exception: true)
310 if Primitive.mandatory_only?
311 Primitive.rb_f_integer1(arg)
313 Primitive.rb_f_integer(arg, base, exception);