File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
tutorials/learnrubyonline.org/en Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ Tutorial
2
+ --------
3
+
4
+ Printing variables and strings
5
+
6
+ You've learned how to use and print variables and strings. But what if you wanted to print a phrase using variables and strings together?
7
+
8
+ In Ruby you can call a variable inside the string what you want to print.
9
+
10
+ To call the variable you neet to use ` #{variable} ` . For example:
11
+
12
+ number = 8
13
+ puts "I have #{number} oranges." # I have 8 oranges
14
+
15
+ You can even make math operations between variables inside it.
16
+
17
+ first_number = 5
18
+ second_number = 6
19
+
20
+ puts "Multiplying #{first_number} by #{second_number} the result is #{first_number * second_number}"
21
+
22
+
23
+ Exercise
24
+ --------
25
+ Modifiy the code to print using the variables:
26
+ - How many apples each of the kids have;
27
+ - The sum of all the apples.
28
+
29
+ Tutorial Code
30
+ -------------
31
+ john = 5
32
+ mary = 3
33
+ will = 2
34
+
35
+ puts "John has apples."
36
+ puts "Mary has apples."
37
+ puts "Will has apples."
38
+ puts "Together they have apples."
39
+
40
+ Expected Output
41
+ ---------------
42
+ John has 5 apples.
43
+ Mary has 3 apples.
44
+ Will has 2 apples.
45
+ Together they have 10 apples.
46
+
47
+
48
+ Solution
49
+ --------
50
+ john = 5
51
+ mary = 3
52
+ will = 2
53
+
54
+ puts "John has apples."
55
+ puts "Mary has apples."
56
+ puts "Will has apples."
57
+ puts "Together they have apples".
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ learnrubyonline.org is still under construction - If you wish to contribute tuto
15
15
- [[ Variables and Types]]
16
16
- [[ Math]]
17
17
- [[ Strings]]
18
+ - [[ Printing variables and strings]]
18
19
- [[ Arrays]]
19
20
- [[ Hashes and Symbols]]
20
21
- [[ Conditional Statements]]
You can’t perform that action at this time.
0 commit comments