Ruby Intro

SENG2021

Strings

There isn’t much special about Ruby strings. Try the code snippets below in irb.

Construction

Strings can be created either using literals, or the new function.

1
2
3
4
5
6
7
8
9
a = "abcde"
puts a
# abcde
# => nil

b = String.new "fghij"
puts b
# fghij
# => nil

Instance Functions

Like java.lang.String, there are loads of predefined methods for the class. Remeber to refer to the Class documentation; there are code samples in there too. I’m just going to give you a taste of what’s available.

1
2
3
4
5
6
7
8
9
10
a.length
# 1
a.next
# abcdf
a.reverse
# edcba
a.upcase
# ABCDE
a.slice(3, 5)
# de

You’ll notice that the methods above only returns the result of the function. Some methods makes changes to the string in place. In general, the in place version will end with an exclamation mark, but note that this is not a standard.

1
2
3
4
5
6
7
8
c = "ababab"
c.reverse!
puts c
# bababa

c.upcase!
puts c
# BABABA

Like Perl, Strings in Ruby are mutable. The object_id method I’m using below returns the id of the object, similar to the & operator in C. Strings inherit this from the Object class. Note that String objects are created every time a String literal is used.

1
2
3
4
5
6
7
8
9
10
11
12
"xyzxyz".object_id
# 70243133245180
"xyzxyz".object_id
# 70243133228560

d = "xyzxyz"
d.object_id
# 70243133212180
d.reverse!
# "zyxzyx"
d.object_id
# 70243133212180

Building a String

Strings can be built from variables in 4 different ways. The first two is probably the most commonly used

1
2
3
4
5
6
7
8
e = "abc"
f = "def"
g = 10
h = e + " + " + f + " is not equal to " + g.to_s
i = "#{e} + #{f} is not equal to #{g}"
puts h
puts i
# abc + def is not equal to 10

The third way is appending to strings. It’s more efficient in that it simply adds to a string instead of creating a new one, but it does change the string that is being appended to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
j = e << " + " << f << " is not equal to " << g.to_s
# abc + def is not equal to 10

j.object_id
# 70243124981460
e.object_id
# 70243124981460
f.object_id
# 70243124965700

puts e
# abc + def is not equal to 10
puts f
# def

The first line basically means, append “ + ” to e, then append f to e, then append “ is not equal to ” to e, and finally append g.to_s to e. I will demonstrate the final way when we get to the Arrays section.

Escaping characters

If the String you’re after is literally #{e} + #{f} is not equal to #{g}, ie. you don’t want Ruby to evaluate e, f, and g, you can escape the #{} blocks by prepending the # with a backslash \. Alternatively, you can use single quotes instead of double quotes, and Ruby will escape those characters for you.

1
2
3
4
5
6
puts "\#{e} + \#{f} is not equal to \#{g}"
# #{e} + #{f} is not equal to #{g}
puts '#{e} + #{f} is not equal to #{g}'
# #{e} + #{f} is not equal to #{g}
a = '#{e} + #{f} is not equal to #{g}'
# "\#{e} + \#{f} is not equal to \#{g}"

Ruby Documentation

There’s two way to access the API documentation, online through http://ruby-doc.org/, or offline in your command line with ri.

1
2
ri String
ri String#object_id

Unofficially, (if you don’t already) you’ll learn to love Google and Stack Overflow too :)

  1. Ruby Docs – String