|
| 1 | +# Assigning data |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +*You will learn:* |
| 6 | +- what a variable is |
| 7 | +- three basic Ruby data types |
| 8 | +- how you can assign data to a variable |
| 9 | +- how to receive and use the assigned data again |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Variables |
| 14 | + |
| 15 | +Running a programm is all about analyzing and manipulating data. |
| 16 | +If you want to manipulate the same data multiple times, you have to store it |
| 17 | +somewhere and access it when you need it. For this temporary storage of data |
| 18 | +you use data containers, so called *variables*. |
| 19 | + |
| 20 | +The name of a variables is usually underscore-separated downcased words, like: |
| 21 | + |
| 22 | +`title`, `my_name`, `a_very_long_variable_name` (but try to keep it short!) |
| 23 | + |
| 24 | +Variables that are not meant to be changed after they once got a value are |
| 25 | +called *constants* and, by convention, have uppercase names, like `DEFAULT_NAME`. |
| 26 | + |
| 27 | +Ruby is *case sensitive*, so it cares about capitalization of your variable names. |
| 28 | +This means: `my_name` would be another variable than `my_Name`. |
| 29 | + |
| 30 | +## Three basic data types |
| 31 | + |
| 32 | +Now, we want to have a look at three basic data types in Ruby. We will see more |
| 33 | +types later on. |
| 34 | + |
| 35 | +*1. Strings:* A String represents a text or character and can be simply build by |
| 36 | +writing text between two single quotes (*''*) or double quotes (*""*), e.g. |
| 37 | + |
| 38 | +`'This is a string.'`, `'&'` or `"This too :)!"` |
| 39 | + |
| 40 | +*2. Numbers:* Numbers can be build by just typing the number. Ruby uses several |
| 41 | +number classes internally which you don't have to care about for now. |
| 42 | +If you type a number, Ruby does all the work for you and uses the appropriate |
| 43 | +class for the number. For instance, if you type `1` it will give you a *Fixnum*, |
| 44 | +if you type `1.5` it will give you a *Float*. |
| 45 | + |
| 46 | +*3. Booleans:* A Boolean represents a binary state. A Boolean's value can be |
| 47 | +either *true* or *false*. Keep in mind not to put quotes around *true* or *false* |
| 48 | +if you want to create a Boolean, else it would be a String! |
| 49 | + |
| 50 | +## Variable = 'value' |
| 51 | + |
| 52 | +You can assign data to a variable by using the equals sign `=`. |
| 53 | +E.g. `learned = true` assigns the value *true* to the variable *learned*. |
| 54 | + |
| 55 | +## Getting the data of a variable |
| 56 | + |
| 57 | +After assigning a value to a variable you can acces the value again by typing |
| 58 | +the variable's name. |
| 59 | + |
| 60 | +Typing `learned` will give you the value *true* (which is a Boolean, remember?). |
| 61 | + |
| 62 | +Let us now create some variables and print their values: |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +Set the following variables: |
| 67 | + |
| 68 | +` variable_types` to the number *3* |
| 69 | +` language` to the string *'Ruby'* and |
| 70 | +` learned` to the boolean *true* |
| 71 | + |
| 72 | +and print out each variable's value by using the *puts* method! |
| 73 | + |
| 74 | +--- |
0 commit comments