1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/integer.rb
卜部昌平 bf3f03c925 integer.rbinc: do not generate C functions
This changeset changes for instance Integer#integer? from:

    == disasm: #<ISeq:to_i@<internal:integer>:66 (66,2)-(69,5)> (catch: FALSE)
    0000 opt_invokebuiltin_delegate_leave       <builtin!_bi7/0>, 0       (68)[LiCa]
    0003 leave                                                            (69)[Re]

to:

    == disasm: #<ISeq:to_i@<internal:integer>:66 (66,2)-(69,5)> (catch: FALSE)
    0000 putself                                                          (68)[LiCa]
    0001 leave                                                            (69)[Re]

which is of course faster.
2020-07-02 14:34:30 +09:00

84 lines
1.6 KiB
Ruby

class Integer
def abs
Primitive.attr! 'inline'
Primitive.cexpr! 'rb_int_abs(self)'
end
def bit_length
Primitive.attr! 'inline'
Primitive.cexpr! 'rb_int_bit_length(self)'
end
# call-seq:
# int.even? -> true or false
#
# Returns +true+ if +int+ is an even number.
def even?
Primitive.attr! 'inline'
Primitive.cexpr! 'int_even_p(self)'
end
# call-seq:
# int.integer? -> true
#
# Since +int+ is already an Integer, this always returns +true+.
def integer?
return true
end
def magnitude
Primitive.attr! 'inline'
Primitive.cexpr! 'rb_int_abs(self)'
end
# call-seq:
# int.odd? -> true or false
#
# Returns +true+ if +int+ is an odd number.
def odd?
Primitive.attr! 'inline'
Primitive.cexpr! 'rb_int_odd_p(self)'
end
# call-seq:
# int.ord -> self
#
# Returns the +int+ itself.
#
# 97.ord #=> 97
#
# This method is intended for compatibility to character literals
# in Ruby 1.9.
#
# For example, <code>?a.ord</code> returns 97 both in 1.8 and 1.9.
def ord
return self
end
# call-seq:
# int.to_i -> integer
#
# Since +int+ is already an Integer, returns +self+.
#
# #to_int is an alias for #to_i.
def to_i
return self
end
# call-seq:
# int.to_int -> integer
#
# Since +int+ is already an Integer, returns +self+.
def to_int
return self
end
# call-seq:
# int.zero? -> true or false
#
# Returns +true+ if +int+ has a zero value.
def zero?
Primitive.attr! 'inline'
Primitive.cexpr! 'int_zero_p(self)'
end
end