DEV Community

Shannon Bentley
Shannon Bentley

Posted on

Ruby: Integer & Float Class

Integer Class

In Ruby, integer classes are a part of the core class hierarchy and represent numeric values without decimal points. Ruby provides several integer classes, each with its own set of methods and behaviors.

Integer constructors: You can create integers using constructor methods provided by the Integer class. For example:

Using Integer() method

x = Integer("42") # Converts a string to an integer
Using to_i method:
Enter fullscreen mode Exit fullscreen mode
x = "42".to_i # Converts a string to an integer
Enter fullscreen mode Exit fullscreen mode

Integer literals: You can directly assign integer values to variables using their numeric representation. Ruby supports decimal, octal, hexadecimal, and binary literals.

Decimal: This is the most common way of representing integers. This type of decimal will always be a whole number. There is another class that represents decimal point integers.

For example:

x = 42
Enter fullscreen mode Exit fullscreen mode

Usage

Mathematical Operations: Integers are commonly used in arithmetic operations such as addition, subtraction, multiplication, and division.

x = 10
y = 20
sum = x + y # sum will be 30
Enter fullscreen mode Exit fullscreen mode

Comparison Operations: Integers can be compared using relational operators like <, <=, >, >=, and equality operators like ==, !=.

x = 10
y = 20
puts x < y # Output: true
Enter fullscreen mode Exit fullscreen mode

Iteration: Integers are often used for looping constructs like for loops or iterators such as times.

5.times do |i|
    puts i
end
Enter fullscreen mode Exit fullscreen mode

Indexing and Array Access: Integers are used as indices to access elements in arrays or other data structures.

arr = [10, 20, 30, 40, 50]
puts arr[2] # Output: 30
Enter fullscreen mode Exit fullscreen mode

Conversion: Integers can be converted to strings (to_s) or other numeric types like floats (to_f) if needed.

x = 42
str = x.to_s # Convert integer to string
Enter fullscreen mode Exit fullscreen mode

Float Class

In Ruby, floats are numeric data types used to represent numbers with decimal points. Floats, short for floating-point numbers, allow for the representation of both integer and fractional parts of numbers.

Float literals: You can directly assign float values to variables using their numeric representation. Floats can be written in decimal notation or scientific notation.

Decimal notation: Floats written with a decimal point. For example:

x = 3.14
Enter fullscreen mode Exit fullscreen mode

Float constructors: You can create floats using constructor methods provided by the Float class. For example:

Using Float() method

x = Float("3.14")
Using to_f method:
Enter fullscreen mode Exit fullscreen mode
x = "3.14".to_f
Enter fullscreen mode Exit fullscreen mode

Integer & Float Methods

.Math.sqrt(): Will take a number and respond with the square root of that number; can be used with integers and floats

.to_i: Converts strings and floats into whole number integers

.to_f: Converts whole numbers into floats

.round (): Rounds a float to the nearest decimal point; use a number to get the specific decimal point

.rand(): Prints a random number between what you want it to be; know that the index 0 counts as 1 and the last number will not be used in the code

.odd?: Checks if a number is odd

.even?: Checks if a number is even

Happy coding!

theGlamTechie

Top comments (0)