Skip to content

Commit 4ce3787

Browse files
committed
Add 'Assigning data' unit
1 parent fad206a commit 4ce3787

3 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
variable_types = 3
2+
language = 'Ruby'
3+
learned = true
4+
5+
puts variable_types
6+
puts language
7+
puts learned
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'rspec'
2+
3+
describe 'Your code' do
4+
5+
describe do
6+
before do
7+
allow($stdout).to receive(:puts) {''}
8+
allow($stdout).to receive(:p) {''}
9+
allow($stdout).to receive(:print) {''}
10+
end
11+
12+
it 'defines a variable "variable_types" with the value 3' do
13+
[['solution::code']]
14+
expect(!!defined?(variable_types)).to be true
15+
expect(variable_types).to eq 3
16+
end
17+
18+
it 'defines a variable "language" with the value \'Ruby\'' do
19+
[['solution::code']]
20+
expect(!!defined?(language)).to be true
21+
expect(language).to eq 'Ruby'
22+
end
23+
24+
it 'defines a variable "learned" with the value true' do
25+
[['solution::code']]
26+
expect(!!defined?(learned)).to be true
27+
expect(learned).to eq true
28+
end
29+
end
30+
31+
it 'puts the values of all defined variables to the standard output' do
32+
expect($stdout).to receive(:puts).with(3)
33+
expect($stdout).to receive(:puts).with('Ruby')
34+
expect($stdout).to receive(:puts).with(true)
35+
[['solution::code']]
36+
end
37+
end
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)