← Back to Ruby Operators

Forward Slash

The forward slash (/) is used in Ruby primarily for division and regex (regular expression) literals. We'll also see it used to append Pathname parts.

Division

We can use the forward slash to divide two numbers, such as integers or floats.

> 10 / 2
=> 5
> 10.0 / 3
=> 3.3333333333333335

Head over to the division operator page for more details on how division works in Ruby.

Regex Literals

The standard way to define a regex literal is by wrapping the pattern in a pair of / characters.

> email_regex = /\A.*@.*\z/
=> /\A.*@.*\z/
> email_regex.match('[email protected]')
=> #<MatchData "[email protected]">
> email_regex.match('Bob Burgers')
=> nil

Head over to the regex literal page for more details on how regex literals work in Ruby.

Append to a Pathname

> base_path = Pathname('~/')
=> #<Pathname:~/>
> base_path / 'code' / 'til'
=> #<Pathname:~/code/til>

References