📅  最后修改于: 2023-12-03 14:56:47.979000             🧑  作者: Mango
Ruby is a programming language that supports integers as one of its data types. With Ruby's built-in Integer class, you can perform various operations on integers. In this article, we will explore some of the most commonly used Integer methods in Ruby.
to_s
methodThe to_s
method is used to convert integers to strings. This method returns the string representation of an integer.
num = 42
str = num.to_s
puts str # Output: "42"
to_f
methodThe to_f
method is used to convert integers to floating-point numbers. This method returns the float representation of an integer.
num = 42
float = num.to_f
puts float # Output: 42.0
to_c
methodThe to_c
method is used to convert integers to complex numbers. This method returns the complex representation of an integer.
num = 42
complex = num.to_c
puts complex # Output: (42+0i)
+
methodThe +
method is used to add two integers.
a = 42
b = 10
sum = a + b
puts sum # Output: 52
-
methodThe -
method is used to subtract two integers.
a = 42
b = 10
difference = a - b
puts difference # Output: 32
*
methodThe *
method is used to multiply two integers.
a = 42
b = 10
product = a * b
puts product # Output: 420
/
methodThe /
method is used to divide two integers. If both the numerator and the denominator are integers, the result of the division operation will be an integer.
a = 42
b = 10
quotient = a / b
puts quotient # Output: 4
%
methodThe %
method is used to get the remainder when one integer is divided by another integer.
a = 42
b = 10
remainder = a % b
puts remainder # Output: 2
**
methodThe **
method is used to raise an integer to a power.
a = 5
b = 3
power = a ** b
puts power # Output: 125
==
methodThe ==
method is used to compare two integers for equality.
a = 42
b = 42
puts a == b # Output: true
<
methodThe <
method is used to compare two integers to see if the first integer is less than the second integer.
a = 10
b = 42
puts a < b # Output: true
>
methodThe >
method is used to compare two integers to see if the first integer is greater than the second integer.
a = 10
b = 42
puts a > b # Output: false
<=
methodThe <=
method is used to compare two integers to see if the first integer is less than or equal to the second integer.
a = 42
b = 42
c = 10
puts a <= b # Output: true
puts b <= c # Output: false
>=
methodThe >=
method is used to compare two integers to see if the first integer is greater than or equal to the second integer.
a = 42
b = 42
c = 10
puts a >= b # Output: true
puts b >= c # Output: true
&
methodThe &
method is used to perform a bitwise AND operation between two integers.
a = 42
b = 10
result = a & b
puts result # Output: 10
|
methodThe |
method is used to perform a bitwise OR operation between two integers.
a = 42
b = 10
result = a | b
puts result # Output: 42
^
methodThe ^
method is used to perform a bitwise XOR operation between two integers.
a = 42
b = 10
result = a ^ b
puts result # Output: 32
~
methodThe ~
method is used to perform a bitwise NOT operation on an integer. This method returns the one's complement of an integer.
a = 42
result = ~a
puts result # Output: -43
<<
methodThe <<
method is used to shift the bits of an integer to the left by a specified number of bits.
a = 42
result = a << 2
puts result # Output: 168
>>
methodThe >>
method is used to shift the bits of an integer to the right by a specified number of bits.
a = 42
result = a >> 2
puts result # Output: 10
These are some of the most commonly used Integer methods in Ruby. With these methods, you can perform various operations on integers and manipulate data in your Ruby programs. Happy coding!