| 
 | 1 | +goals do  | 
 | 2 | +  goal "Know the basic data types"  | 
 | 3 | +  goal "Know why it matters"  | 
 | 4 | +  message "Variables are easygoing about the objects they hold. You can assign a word, then assign a number, then a list of words."  | 
 | 5 | +  message "But sometimes it matters what type of data we assign a variable. The fancy word for \"type of data\" is **class**."  | 
 | 6 | +  message "Ruby has several built-in classes. In a later section, we'll explore how to make your own classes."  | 
 | 7 | +end  | 
 | 8 | + | 
 | 9 | +step do  | 
 | 10 | +  message "How can you find out what class something is?"  | 
 | 11 | +  irb '"Letters and words".class'  | 
 | 12 | +  result "String"  | 
 | 13 | +  message "\"class\" is a **method**, a behavior you can ask an object to perform. "  | 
 | 14 | +  message "To call a method on an object, type the object or variable name, then  | 
 | 15 | +    a period, and finally the method name. "  | 
 | 16 | +  message "Here's a preview of the datatypes we'll cover:"  | 
 | 17 | +  irb <<-TRYME  | 
 | 18 | +'a'.class  | 
 | 19 | +5.class  | 
 | 20 | +3.14.class  | 
 | 21 | +true.class  | 
 | 22 | +:symbol.class  | 
 | 23 | +[ 1, 2, 3].class  | 
 | 24 | +{name: "value"}.class  | 
 | 25 | +nil.class  | 
 | 26 | +Class.class  | 
 | 27 | +TRYME  | 
 | 28 | +end  | 
 | 29 | + | 
 | 30 | +step do  | 
 | 31 | +  message "Different classes come with different methods. Browse the String  | 
 | 32 | +    class' methods. (Ruby will put a ':' before the method name.)"  | 
 | 33 | +  irb '"five".methods'  | 
 | 34 | +  message "There are a lot of them. Can you predict what some of them do?"  | 
 | 35 | +  message "Numbers have different methods."  | 
 | 36 | +  irb "5.methods"  | 
 | 37 | +  message "Notice that number methods include arithmetic operators."  | 
 | 38 | +  message "Knowing what class an object is tells us what methods we can call."  | 
 | 39 | +end  | 
 | 40 | + | 
 | 41 | +step do  | 
 | 42 | +  message "Some datatypes are incompatible.  For example, Ruby knows how to  | 
 | 43 | +    concatenate two objects of the same class."  | 
 | 44 | +  irb <<-TRYME  | 
 | 45 | +5 + 5  | 
 | 46 | +"5" + "5"  | 
 | 47 | +TRYME  | 
 | 48 | +  message "Ruby can guess how to convert some classes into a similar class."  | 
 | 49 | +  irb "5 + 5.0"  | 
 | 50 | +  message "... but it's totally confused by different objects."  | 
 | 51 | +  irb <<-TRYME  | 
 | 52 | +5 + "5"  | 
 | 53 | +"5" + 5  | 
 | 54 | +TRYME  | 
 | 55 | +end  | 
 | 56 | + | 
 | 57 | +step do  | 
 | 58 | +  message "When you have incompatible data types, you can use an object's  | 
 | 59 | +    *conversion methods* to get a translated version of the object."  | 
 | 60 | +  message "Conversion methods can be spotted by their name; they begin with  | 
 | 61 | +    `to_`, e.g. `to_s` converts to a string, `to_i` to an integer, and `to_f` to  | 
 | 62 | +    a float (decimal)."  | 
 | 63 | +  irb <<-TRYME  | 
 | 64 | +5.to_s  | 
 | 65 | +5.to_s.class  | 
 | 66 | +5.to_f  | 
 | 67 | +5.to_f.class  | 
 | 68 | +5.67.to_i  | 
 | 69 | +"5".to_i  | 
 | 70 | +"5".to_i.class  | 
 | 71 | +TRYME  | 
 | 72 | +  message "There isn't always a sensible conversion from one class to another."  | 
 | 73 | +  irb <<-TRYME  | 
 | 74 | +"I am not a number".to_i  | 
 | 75 | +"five".to_i  | 
 | 76 | +TRYME  | 
 | 77 | +  message "Conversion methods are used most often in calculations and in printing."  | 
 | 78 | +  irb <<-TRYME  | 
 | 79 | +5 + "5".to_i  | 
 | 80 | +"5 plus" + 5.to_s  | 
 | 81 | +TRYME  | 
 | 82 | +  message "It's common to include a `to_s` method when you make your own classes so it prints nicely."  | 
 | 83 | +  message "To explore further, try converting the examples in step 1 to  | 
 | 84 | +    different classes.  To more easily see what conversion methods an object has,  | 
 | 85 | +    call `methods.sort` on it."  | 
 | 86 | +  irb "5.methods.sort"  | 
 | 87 | +end  | 
 | 88 | + | 
 | 89 | +explanation do  | 
 | 90 | +  message "Sometimes it's necessary to think about what type of data you're working with."  | 
 | 91 | +  message "Use the `class` method to find out what class an object is."  | 
 | 92 | +  message "Use the `methods` method to find out what methods an object has."  | 
 | 93 | +  message "Use conversion methods to get a transformed version of an object."  | 
 | 94 | +  message "A `TypeError` message is a hint to look more carefully at an object's class."  | 
 | 95 | +  message "Let's look at the basic datatypes in more detail."  | 
 | 96 | +end  | 
 | 97 | + | 
 | 98 | +next_step "strings"  | 
0 commit comments