-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson 1 | Basics
134 lines (94 loc) · 2.16 KB
/
Lesson 1 | Basics
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
LITERALS
----------------------------------------------
'Hello world' // string
375 // integer
3.14 // float
true // boolan
{ 'a' => 1, 'b' =>2 } // hash
[ 1,2,3 ] // array
:sym // symbol
nil //nil
STRINGS
------------------------------------------------
"The man said 'hi' ".
' The man said \'hi there \' '
irb :001 >a = 'ten'
=> "ten"
irb :002 => "my favorite number is #{a}"
=> "my favorite number is ten! "
String interpolation only works within double quotes
SYMBOLS
------------------------------------------------
:name
:a_symbol
:"suprisingly, this is a symbol"
NOTE: symbol is used when you want to reference something like a string but don't ever intend to print it to the screen or change it.
NUMBERS
------------------------------------------------
#integer and float
# 1,2,3 | 1.1, 1.2, 1.3
NULL / Nill
------------------------------------------------
irb: 001> puts "hello world"
Hello, world
=> nill
NOTE: puts method prints out a string and returns nothing
irb: 002 > x = nill
=> nill
### check if something is nil or not ###
irb :001 > "hi" .nill?
=> false
An important property of the nil type is that when used in an expression, such as an if statement, it will be treated as false,
irb :001 > if nill
irb :002 > puts "hello world"
irb :003 > end
=> nil
NOTE: nil can be used within a conditional statement, and will be treated as false.
irb :001 > false == nil
=> false
OPERATIONS
------------------------------------------------
# Adding, Subtracting, Multiplying integers
irb :001 > 1 + 1
=> 2
irb :001 > 1 - 1
=> 0
irb :001 > 2 * 2
=> 4
irb :001 > 16 / 4
=> 4
irb :001 > 16 % 4
=> 0
irb :001 > 16.remainder(5)
=> 1
irb :001 > 16.divmod(5)
=> \[3, 1\]
irb :001 > 4 ==4
=> true
irb :002 > 4 == 5
=> false
irb :001 > foo == 'foo'
=> true
irb :002 > 'foo' == 'bar'
=> false
irb :002 > '4' == 4
=> false
// String concatenation
irb :001 > '1' + '1'
=> '11'
irb :002 > 'one' + 1
=> TypeError
TYPE CONVERSION
------------------------------------------------
irb :001 > '12'.to_i
=> 12
irb :002 > '4 hi there'.to_i
=> 4
irb :003 > 'hi there 4'.to_i
=> 0
irb :004 > '4'.to_f
=> 4.0
irb :005 > '4 hi there'.to_f
=> 4.0
irb :006 > 'hi there 4'.to_f
=> 0.0