ESC
Type to search...
S
Soli Docs

Integers

64-bit signed integers with built-in methods for arithmetic, iteration, and type inspection.

Creating Integers

let x = 42
let negative = -100
let big = 9_000_000       # Underscores for readability
let typed: Int = 10

Arithmetic Methods

.abs

Returns the absolute value.

(-42).abs    # 42
5.abs        # 5
.pow(exponent)

Raises the integer to the given power.

2.pow(10)    # 1024
3.pow(3)     # 27
2.pow(-1)    # 0.5 (returns float for negative exponents)
.sqrt

Returns the square root as a float.

16.sqrt      # 4.0
2.sqrt       # 1.4142135623730951
.gcd(other)

Returns the greatest common divisor.

12.gcd(8)    # 4
15.gcd(10)   # 5
.lcm(other)

Returns the least common multiple.

4.lcm(6)     # 12
3.lcm(5)     # 15

Predicate Methods

.even? / .odd?

Check if the integer is even or odd.

4.even?      # true
4.odd?       # false
7.even?      # false
7.odd?       # true
.zero?

Check if the integer is zero.

0.zero?      # true
5.zero?      # false
.positive? / .negative?

Check if the integer is positive or negative.

5.positive?     # true
(-3).negative?  # true
0.positive?     # false
0.negative?     # false
.between?(min, max)

Check if the integer is within the given range (inclusive).

5.between?(1, 10)    # true
15.between?(1, 10)   # false
.clamp(min, max)

Constrain the integer to be within the given range.

15.clamp(1, 10)   # 10
(-5).clamp(0, 100) # 0
5.clamp(0, 100)    # 5

Iteration Methods

.times |i| ... end

Execute a block n times, passing the index (0 to n-1).

3.times |i|
    println(i)
end
# 0
# 1
# 2
.upto(limit) |i| ... end

Iterate from the integer up to the limit (inclusive).

1.upto(5) |i|
    print(i + " ")
end
# 1 2 3 4 5
.downto(limit) |i| ... end

Iterate from the integer down to the limit (inclusive).

5.downto(1) |i|
    print(i + " ")
end
# 5 4 3 2 1

Conversion Methods

.to_s / .to_string

Convert to string representation.

42.to_s      # "42"
(-7).to_string  # "-7"
.to_f / .to_float

Convert to floating-point number.

42.to_f      # 42.0
(-7).to_float   # -7.0
.chr

Convert a Unicode code point to a character string.

65.chr       # "A"
97.chr       # "a"
9829.chr     # "♥"

Introspection Methods

.class

Returns the type name as a string.

42.class     # "int"
.inspect

Returns a developer-friendly string representation.

42.inspect   # "42"
.is_a?(type_name)

Check if the value is of the given type. Supports "int", "numeric", and "object".

42.is_a?("int")       # true
42.is_a?("numeric")   # true
42.is_a?("string")    # false
.nil?

Returns false (integers are never nil).

42.nil?      # false
.blank? / .present?

Integers are always present (never blank).

42.blank?    # false
42.present?  # true
0.blank?     # false
0.present?   # true