Skip to content

Integer functions

abs

Returns the absolute (non-negative) value of the integer.

<%= -5.abs %>
5

to_string

Converts the integer to a string. to_s is an alias for to_string.

<%= 5.to_string + " times" %>
5 times

to_float

Converts the integer to a floating point number. to_f is an alias for to_float.

<%= 5.to_float * 2.5 %>
12.5

times

Creates a list of integers, starting at 0 and ending with the integer. This function is commonly used to run a for loop, for example:

<% for i in 3.times %>
    <%= i %>.
<% end %>
1.
2.
3.

clamp_zero

Clamps the integer to 0, i.e. negative values become 0.

<%= -25.clamp_zero %>
0

clamp_one

Clamps the integer to 1, i.e. all values less than 1 become 1.

<%= 0.clamp_one %>
1

replace

Replaces matching integer(s) in the number. sub is an alias for replace.

<%= 15345.replace(5, 6) %>
16346