rubyisforfun/manuscript/020.txt

58 lines
3.6 KiB
Text
Raw Normal View History

2019-01-05 05:16:13 +01:00
# Part II. Essentials
2019-01-01 00:15:50 +01:00
## Data Types
2019-01-05 06:36:19 +01:00
In previous chapters we had 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:
2019-01-01 00:15:50 +01:00
{lang=ruby, line-numbers=off}
```ruby
$ irb
> "blabla".class
=> String
> "123".class
=> String
> 123.class
=> Integer
```
2019-01-05 06:36:19 +01:00
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.
2019-01-01 00:15:50 +01:00
2019-01-05 06:36:19 +01:00
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:
2019-01-01 00:15:50 +01:00
```
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 `class`:
```
Object.class
```
2019-01-05 06:36:19 +01:00
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 `.class`
2019-01-01 00:15:50 +01:00
2019-01-05 06:36:19 +01:00
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:
2019-01-01 00:15:50 +01:00
* [Object](https://ruby-doc.org/core-2.5.1/Object.html)
2019-01-03 05:05:03 +01:00
* [String](https://ruby-doc.org/core-2.5.1/String.html)
* [Integer](https://ruby-doc.org/core-2.5.1/Integer.html)
2019-01-01 00:15:50 +01:00
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.
2019-01-05 06:36:19 +01:00
Useful links:
2019-01-01 00:15:50 +01:00
2019-01-05 06:36:19 +01:00
* [Documentation example for Object.class](https://ruby-doc.org/core-2.5.1/Object.html#method-i-class)
* [Documentation example about multiplying string by a number](https://ruby-doc.org/core-2.5.1/String.html#method-i-2A) -- don't miss out interesting example of multiplying string by a zero (result is empty string).
2019-01-01 00:15:50 +01:00
There are also many other types of objects in Ruby, most important of them will be covered in the next chapters.
X> ## Exercise
2019-01-05 06:36:19 +01:00
X> 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: `3.14`?
2019-01-01 00:15:50 +01:00
X> ## Exercise
X> We know that `.class` method 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 `.class` will return? Try to find this out by putting `.class` right after `123.class` this way: `123.class.class`. First part of this expression will return result. And the second part will return type of this result.