-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathruby_cheat_sheet.rb
289 lines (198 loc) · 5.3 KB
/
ruby_cheat_sheet.rb
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#variables
variable = some_value
name = "Toni"
sum = 18 + 5
#console output
puts "Hello World"
puts [1,5, "moo"]
#call a method - or send a message
#sending an object a message & waiting for its response
#can have no arguments or multiple arguments
object.method(arguments)
string.length
array.delete_at(2)
string.gsub("ae","a")
#substitute all "ae" occurences with "a"
#define a method
def name(parameter)
#method body
end
def greet(name)
puts "Hi there" + name
end
greet("Tobi")
#methods are reusable units of behaviour
#methods are small & focused on implementing a specific behaviour
#Equality
object == other
true == true # => true
#check if two things are the same, true will be returned if so
3 == 4 # => false
"Hello" == "Hello" # => true
"Helo" == "Hello" # => false
#Inequality
#the inverse of equality, results in true when two values are not the same
#results false when rhet are the same
object != other
true != true # => false
3 != 4 # => true
#Decisions with if
#with an if-clause decide based upon a condition what to do
#when condition is true, code after if statement is executed
if condition
# happens when true
else
# happens when false
end
if input == password
grant_access
else
deny_access
end
#if input matches password access is granted
#constant
#constants look like variables but in UPCASE
#both hold values & give you a name to refer to those values
#difference is constants value are known & should never change- always refer to same values
CONSTANT = some_value
ADULT_AGE = 18
#Numbers
number_of_your_choice
0, -11, 42
#Decimals
main.Decimal
3.2
#-5.0
#> 1.0 + 2
#=> 3.0
#> 1 + 2.0
#=> 3.0
#Mathematical operations result in a floating point number except if all numbers used are integer numbers.
#Basic math
n operator m
2 + 3 # => 5
5 - 7 # => -2
8 * 7 # => 56
84 / 4 # => 21
#compariosn
n operator m
12 > 3 # => true
12 < 3 # => false
7 >= 7 # => true
ADULT_AGE = 18
ADULT_AGE >= 18 # true
#Strings
#recommends using '' single quotes for simple strings
'Hello World'
'a'
'Just'
#Interpolation
#"A string and an #{expression}"
"Email: #{user.email}"
"The total is #{2 + 2}"
#combine a string with a variable or Ruby expression using ""
#length
string.length
"Hello".length # => 5
"".length # => 0
#concatenate
string + string2
"Hello" + "reader"
#=> "Hello reader"
"a" + "b" + "c" #=> "abc"
#substitute
string.gsub(a_string, substitute)
#gsub - stands for "globally substitute"- substitutes all occurences of a_string
#within the string with substitute
"Hae".gsub("ae", "a")
#=> "Ha"
"Hae".gsub("b", "a")
#=> "Hae"
"Greenie".gsub("e", "u")
#=> "Grunniu"
#string[position] "Hello"[1] #=> "e"
#access the character at the given position in the string, first position is 0
#Arrays
#array conains multiple objects that are mostly related to each other
#create
[contents]
["Rails", "fun", 5]
#number of elements
array.size
[].size #=> 0
[1,2,3].size #=> 3
["foo", "bar"].size #=> 2
#access
array[position]
array = ["hi", "foo", "bar"]
array[0] #=> "hi"
array[2] #=> "bar"
#adding an element
#add elements to the end of the array, increasing size of array by one
array << element
array = [1, 2, 3]
array << 4
array # => [1, 2, 3, 4]
#assigning
array[number] = value
array = ["hi", "foo", "bar"]
array[2] = "new"
array # => ["hi", "foo", "new"]
#delete at index
#deletes array at specified index
array.delete_at(i)
array = [0, 14, 55, 79]
array.delete_at(2)
array # => [0, 14, 79]
#Iterating
#Iterating, doing something for each element of the array
#code placed between do & end determines what is done to each element in the array
array.each do |e| .. end
persons.each do |p| puts p.name end
#prints name of every person in the array to the console
numbers.each do |n| n = n * 2 end
#doubles every number of a given array
#Hashes
#Hashes associate a key to some value. You may then retrieve the value based
#upon its key- called a dictionary in other languages
#use the key to "look up" a value as you would a definition for a word in a dictionary
#each key must be unique for a given hash but values can be repeated
#Hashes can map from anything to anything - can map Strings to Numbers, Strings to Keys,
#Numbers to Booleans
#Common that at least all the keysare of the same class
#Symbols are especially common as keys, symbools look like this :symbol
#use symbols becuase Ruby runs faster when use symbols instead of strings
#Creating
{key => value}
{:hobby => "programming"}
{42 => "answer", "score" => 100,
:name => "Tobi"}
#surrounding key-value pairs with curly braces
#arrow always goes from key to value depicting meaning
#key value pairs separated by commas
#Accessing
hash[key]
#accessing an entry in a hash looks a lot like acceessing it in an array
#with a hash the key can be anything not just numbers
#if you try to access a key that isn't known will return nil
hash = {: key => "value"}
hash [:key] # => "value"
hash[foo] # => nil
#assigning
hash[key] = value
#assigning values to a hash similar to assigning values to an array
#key could be anything (string, number, boolean etc)
hash = {a: a => "b"}
hash[:key] = "value"
hash # => {:a => "b",
:key => "value"}
#Deleting
hash.delete(key)
#delete a specified key from the hash so they can & its value can't be assessed
hash = {:a => "b", :b => 10}
hash.delete(:a)
hash # => {:b => 10}
#.sort!
#modifies the data
#.sort
#fresh copy