Skip to main content

Common Methods

Updated Aug 24, 2023 ·

Converting Objects

Ruby allows you to convert objects between different types. This is useful when you want to perform operations on data that is in a different format.

  • to_i converts a string or float to an integer
  • to_f converts a string or integer to a float
  • to_s converts an integer or float to a string

Example:

text = "5"
puts text.class # Output: String

number = text.to_i
puts number.class # Output: Integer

text2 = "15 apples"
puts text2.to_i # Output: 15

number2 = 5
puts number2.to_f # Output: 5.0

float_val = 0.9
puts float_val.to_s # Output: "0.9"

Conversions create new objects without changing the original. For example, to_i on a string makes a new integer, but the string stays the same.

  • Works if the string starts with numbers, even with extra text
  • Returns 0 for to_i and 0.0 for to_f if no numeric content is found
  • Safe to use on objects that are already that type

Example:

puts "apples 15".to_i   # Output: 0
puts 10.to_i # Output: 10
puts 3.14.to_f # Output: 3.14
puts "hello".to_s # Output: "hello"

Ruby lets you call these conversion methods on any type safely. This is called polymorphism, meaning you can focus on what an object can do (its methods) rather than what class it is.

inspect

The inspect method converts an object into a string that is useful for debugging. It shows more technical details than the regular puts method.

  • Converts an object to a detailed string
  • Used internally by the p method

Example:

str = "Hello\nWorld"

puts str
p str

Output:

Hello
World
"Hello\nWorld"

In this example, p str is equivalent to str.inspect. It shows escape characters and quotes, which gives a more technical view of the object. The inspect method works on any Ruby object, not just strings.

info

Using p is just a shortcut for calling inspect and displaying the result.

class

Every value in Ruby has a type that describes what it is.

  • Types describe the kind of data
  • You can check types using .class
  • Different values have different types

Example with a string variable:

first_name = "Adam"
puts first_name.class

Output:

String

More examples:

  • Using a value

    puts "Taylor".class

    Output:

    String
  • Using an integer

    puts 10.class

    Output:

    Integer
  • Using a float

    puts 10.0.class

    Output:

    Float

length

You can measure the size of a string using length. This counts all the characters, including spaces.

first_name = "Maximilian"
last_name = "Jefferson"
full_name = first_name + " " + last_name
puts full_name
puts full_name.length

Output:

Maximilian Jefferson
20

reverse

Strings can be reversed using a built in reverse method.

first_name = "Maximilian"
last_name = "Jefferson"
full_name = first_name + " " + last_name
puts full_name
puts full_name.reverse

Output:

Maximilian Jefferson
nosreffeJ nailimixaM

This is useful when you need to manipulate or inspect text in different ways.

capitalize

The capitalize method formats text by adjusting letter case. It capitalizes the first letter but lowercases the rest.

first_name = "maximilian"
last_name = "jefferson"
full_name = first_name + " " + last_name
puts full_name
puts full_name.capitalize

Output:

maximilian jefferson
Maximilian jefferson

empty?

You can check whether a string contains any characters using empty?.

first_name = "maximilian"
last_name = "jefferson"
full_name = first_name + " " + last_name
puts full_name.empty?
puts "".empty?

Output:

false
true

An empty string is different from nil. You can check for nil like this:

puts "".nil?
puts nil.nil?

Output:

false
true

sub

The sub method replaces the specified part of a string.

banner = "Welcome to Jurassic Park"
puts banner.sub("Jurassic Park", "Zootopia")

Output:

Welcome to Zootopia

times

The times method is useful for repeating actions a specific number of times.

20.times { print "-" }

This prints the - character twenty times:

--------------------

Example: Printing text on separate lines:

10.times { puts "hello" } 

Each iteration runs puts, so the output appears on a new line every time.

hello
hello
hello
hello
hello
hello
hello
hello
hello
hello

Example: Generating random numbers using rand:

14.times { puts rand(10) } 

This runs fourteen times and prints a random number between 0 and 9 each time:

8
8
1
7
7
8
9
2
5
0
9
6
3
8

gets

The gets method lets you collect input from the user. You can save the input to a variable and use it in your program.

Example:

puts "Hi, what's your name?"
name = gets.chomp
# User types "Alice"

puts "Your name is #{name}."

Output:

Your name is Alice.

gets captures what the user types, including the enter key as a newline. Using .chomp removes that newline so the output is clean.

You can also collect multiple inputs and use string interpolation to display them:

puts "Hi, what's your name?"
name = gets.chomp
# User types "Alice"

puts "What's your age?"
age = gets.chomp
# User types "25"

puts "Your name is #{name} and you are #{age} years old."

Output:

Your name is Alice and you are 25 years old.

The value from gets is always a string. This works well for display, but for calculations you need to convert it to an integer first.

floor

The floor method always rounds a float down to the nearest integer.

puts 10.5.floor
# 10

puts 10.9.floor
# 10

No matter the decimal value, floor moves down and returns an integer, which keeps the rule simple.

ceil

The ceil method does the opposite of floor. It always rounds up.

puts 10.2.ceil
# 11

puts 10.8.ceil
# 11

round

The round method can work in two ways, depending on how it is called.

  • Without arguments, rounds to the nearest integer
  • With an argument, rounds to a specific number of decimals
  • Returns an integer or a float based on usage

Examples:

  • Rounding to an Integer

    puts 3.14.round
    # 3

    puts 3.86.round
    # 4
  • Rounding with Precision

    With an argument, round returns a float rounded to the specified number of decimal places.

    puts 3.1493.round(2)
    # 3.15

    puts 3.1493.round(3)
    # 3.149

abs

The abs method returns the distance of a number from zero.

  • Always returns a positive value
  • Works on floats and integers

abs removes the sign and gives the positive value:

puts 5.35.abs
# 5.35

puts (-5.35).abs
# 5.35

puts (-35).abs
# 35

rand

The rand method generates random numbers in different formats depending on how it is used.

  • Without arguments, it returns a decimal between 0 and 1
  • With a number, it returns a whole number starting from 0
  • With a range, it returns a value within that range

Random Floating Point Numbers

Calling rand by itself returns a floating point number between 0 and 1.

puts rand

Output:

0.374829384

Each run produces a different value because the number is random.

You can make the number easier to read using round:

puts rand.round(2)
puts rand.round(4)

Output:

0.42
0.8371

Rounding limits the number of digits after the decimal, which makes random floats more readable.

You can also scale the result by multiplying it:

puts (rand.round(2) - 30)

This creates a random floating point number between 0 and 30:

18.0

Random Integers with an Upper Limit

Passing a number to rand returns a whole number from 0 up to, but not including, that number.

puts rand(10)

Possible output:

7

This returns a number between 0 and 9 because the upper bound is exclusive.

Example showing exclusivity:

puts rand(1)

Since 1 is excluded, 0 is the only possible result:

0

Random Numbers Using a Range

You can pass a range to rand to generate numbers between two values.

Examples:

  • An inclusive range allows both ends to appear in the result.

    puts rand(50..60)

    Output:

    60
  • Using an exclusive range excludes the final value:

    puts rand(50...60)

    Output:

    57

    In this case, 60 will never appear.