Strings, Data Types and Objects in Ruby
Let’s look at the program from previous chapter. Can we do better?
puts "Your age?" age = gets puts "Your age is" puts age
We can replace line 3 and 4 with a single line. For example:
puts "Your age?" age = gets puts "Your age is" + age
Your age? 30 Your age is30
Something is missing, isn’t it? That’s right, the space is missing between words “is” and “30”. As you can see from example above, we can join two strings. From a purely math point of view adding up two strings is nonsense, but strings do concatenate (join) in computer memory. Run the following code in REPL or as a program:
"My name is " + "Roman" + " and my age is " + "30"
My name is Roman and my age is 30
Now try to add these two numbers, both represented as a string, try to guess what would be the answer?
"100" + "500"
Spoiler: answer is “100500”. In other words, if number is represented as a string (comes in quotes), Ruby will treat this number as a string. But if we just type
100 + 500 (without quotes), produced result will be
It turns out that you can also multiply string by a number:
"10" * 5 => "1010101010"
"10" repeated 5 times. If we leave space after 10, result will be represented in a more illustrative way:
"10 " * 5 => "10 10 10 10 10 "
As it was mentioned before,
"10 " is just a string, and we can use any string we want:
"I'm cool! " * 10 => "I'm cool! I'm cool! I'm cool! I'm cool! I'm cool! I'm cool! I'm cool! I'm cool! I'm cool! I'm cool! "
But in practice, developers often multiply a single character by 80 (legacy text screen width). We can multiply strings like
"-" by 80 to logically separate results from input. For example:
puts "Your age?" age = gets puts "=" * 80 puts "Your age is " + age
Your age? 30 ======================================================================== Your age is 30
We have figured out how to join two strings with
+. Also, we know that we can multiply string by a number. While experimenting we found that there are at least two types of data: strings and numbers (integers). And the number itself, but in quotes, is a string. Let’s see at how Ruby understands what is a number and what is a string:
$ irb > "blabla".class => String > "123".class => String > 123.class => Integer
Documentation says that everything is an object in Ruby. So the result of any operation is object. Every object “implements method” called
class. Expression “implements method” means that some programmer, developer of Ruby language, made a tiny sub-program (or sub-routine) that we can run if we know its name. To call this small program of any object we should type dot and the name of this program.
In our case above the name of this sub-program (or “method”, “function”) is
class. By the way, don’t get confused. There are two things: 1) method
class we call above by specifying dot at the beginning 2)
class keyword, used to define a class of objects – we’ll cover it later in this book. If real-life objects could have methods, we would see something like this:
Apple.slice Apple.amount_of_seeds Apple.amount_of_worms River.water_temperature River.amount_of_fish
And so on. So that’s how every object in Ruby has method
In our experiment at the beginning of this chapter,
123 (without quotes) and
"blabla" are objects. Type of
123 is Integer. And type of
"blabla" is String. Type of any object can be obtained by calling
Of course, every object has documentation with the list of supported methods. We encourage you to lookup documentation every time you have questions, and every time you work with this or another type. Documentation examples for different object types:
Documentation is quite easy to find if you search for “ruby object docs” or “ruby string docs”. Documentation covers everything we can do with an object, and it’s a “gold mine” of information, you shouldn’t ignore it, it should be your best friend. Programmer who doesn’t like or lazy about looking up documentation will hardly ever succeed.
- Documentation example for Object.class
- Documentation example about multiplying string by a number – don’t miss out interesting example of multiplying string by a zero (result is empty string).
There are also many other types of objects in Ruby, most important of them will be covered in the next chapters.
Exercise Open up your REPL and find out the data type for
"". What’s the data type for
0(zero)? What’s the data type for
-1? What is the data type for approximate Pi-number:
Exercise We know that
.classmethod returns some sort of result for any object. REPL reads, evaluates and prints this result. But if everything is an object, it means that result is an object too. But does this object has a type? It should. What type of result method
.classwill return? Try to find this out by putting
123.class.class. First part of this expression will return result. And the second part will return type of this result.
Everything Is An Object
We know that
123.class returns Integer, and
"blabla".class returns String. But any object also has
is_a? method, which returns true or false, depending on parameter you’re passing to this method:
$ irb > 123.is_a?(Integer) => true
In example above we’re calling
is_a? method for
123 object and passing parameter
Integer. Method returns true. In other words,
123 is a type of Integer. The similar way we can test if
$ irb > 123.is_a?(String) => false
Answer is false,
123 is not String, but
"123" (in quotes) is String:
$ irb > "123".is_a?(String) => true > "blabla".is_a?(String) => true
is_a? we’re kinda asking question in plain English “Is this a… ?”, “Is this object a string?”
We’ve confirmed that
123 is Integer, and
"blabla" is String. Now let’s make sure integers and strings are objects:
$ irb > 123.is_a?(Object) => true > "blabla".is_a?(Object) => true
Great, numbers and strings are objects in Ruby! In other words,
123 is Integer and Object at the same time. And
"blabla" is String and Object at the same time. In other words, there can be multiple objects, and objects can implement multiple types.
We’ll discuss later in this book what object really is. We don’t need to remember
is_a? method at the moment, how to call this method, and what it returns (in computer literature it is called “method signature”, sometimes API - Application Program Interface). But it’s worth remembering
.class, so you can any time check the type of any object (or the type of result of some operation).