goal "Know the basic data types"
goal "Know why it matters"
message "Variables are easygoing about the objects they hold. You can assign a word, then assign a number, then a list of words."
message "But sometimes it matters what type of data we assign a variable. The fancy word for \"type of data\" is **class**."
message "Ruby has several built-in classes. In a later section, we'll explore how to make your own classes."
message "How can you find out what class something is?"
irb '"Letters and words".class'
message "\"class\" is a **method**, a behavior you can ask an object to perform. "
message "To call a method on an object, type the object or variable name, then
a period, and finally the method name. "
message "Here's a preview of the datatypes we'll cover:"
[ 1, 2, 3].class
message "Different classes come with different methods. Browse the String
class' methods. (Ruby will put a ':' before the method name.)"
message "There are a lot of them. Can you predict what some of them do?"
message "Numbers have different methods."
message "Notice that number methods include arithmetic operators."
message "Knowing what class an object is tells us what methods we can call."
message "Some datatypes are incompatible. For example, Ruby knows how to
concatenate two objects of the same class."
5 + 5
"5" + "5"
message "Ruby can guess how to convert some classes into a similar class."
irb "5 + 5.0"
message "... but it's totally confused by different objects."
5 + "5"
"5" + 5
message "When you have incompatible data types, you can use an object's
*conversion methods* to get a translated version of the object."
message "Conversion methods can be spotted by their name; they begin with
`to_`, e.g. `to_s` converts to a string, `to_i` to an integer, and `to_f` to
a float (decimal)."
message "There isn't always a sensible conversion from one class to another."
"I am not a number".to_i
message "Conversion methods are used most often in calculations and in printing."
5 + "5".to_i
"5 plus" + 5.to_s
message "It's common to include a `to_s` method when you make your own classes so it prints nicely."
message "To explore further, try converting the examples in step 1 to
different classes. To more easily see what conversion methods an object has,
call `methods.sort` on it."
message "Sometimes it's necessary to think about what type of data you're working with."
message "Use the `class` method to find out what class an object is."
message "Use the `methods` method to find out what methods an object has."
message "Use conversion methods to get a transformed version of an object."
message "A `TypeError` message is a hint to look more carefully at an object's class."
message "Let's look at the basic datatypes in more detail."