Working with Strings

In order to do these exercises you should have read at least the chapter about [built_in_classes/strings.html].

Exercise 2.1

In irb try figuring out how to produce the following String: "Ruby<3<3<3".

Try using no more than two other String objects.

Show solution

To start irb open your terminal and type irb, then hit the return key (enter). To quit it (and get back to your system shell prompt) type exit or press ctrl-d.

Exercise 2.2

Look through the documentation for Strings.

Do you spot any interesting methods?

Exercise 2.3

What do you think this will do?

$ irb
> "hello".length + "world".length

Try it yourself in irb.

Show solution

Exercise 2.4

Look through the documentation for Strings, and find out how to convert the string "1.23" into the number 1.23.

If you can’t find it, google for “ruby convert string to float”.

Then also find out what method can be used to turn the string "1" into the number 1 (remember floats and integers are different kinds of numbers).

Confirm that you have found the right methods by trying them in irb.

Show solution