-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercises.rb
More file actions
250 lines (178 loc) · 5.22 KB
/
Exercises.rb
File metadata and controls
250 lines (178 loc) · 5.22 KB
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
name = "Joseph"
completed_message = "este es un mensaje de #{name}"
new_message = completed_message.gsub("Joseph","John") # To replace and give a copy assigned to new_message
completed_message.gsub!("Joseph","John") # To replace and assign to completed_message, just need to add ! on gsub method
# Symbols
# A symbol is created just one time
# color and color2 have the same object_id, because they point to the same symbol
color = :red
color.object_id
color2 = :red
color2.object_id
# Arrays
array1 = ["a","b","c","d"]
array1.count { |x| x=="c"} # => 1
array2 = [1,2,3,4,5,6,7]
array2.count { |x| x.even?} # => 3
array2.map {|x| x*2} # => [2, 4, 6, 8, 10, 12, 14] .map transform each element to a new array
array2.select {|x| x.even?} # => [2, 4, 6] .select just get the elements on a new array
m1 = "message to test split"
m1.split(" ") # => ["message", "to", "test", "split"]
m1.split(" ").map {|x| x.upcase} # => ["MESSAGE", "TO", "TEST", "SPLIT"]
m1.split(" ").map {|x| x.upcase}.join("-") # => "MESSAGE-TO-TEST-SPLIT"
array3 = ["b","d","a","c"]
array3.sort
# Hashes
capitals = {"Mexico" => "Mexico city"}
capitals["Colombia"] = "Bogota"
puts capitals
capitals.has_value? "Bogota" # true
puts capitals.invert # {"Mexico city"=>"Mexico", "Bogota"=>"Colombia"}
capitals.merge!({"España" => "Madrid"}) # with ! , you apply the change to the current array
puts capitals
capitals.transform_values {|x| x.upcase} #{"Mexico"=>"MEXICO CITY", "Colombia"=>"BOGOTA", "España"=>"MADRID"}
capitals.map {|k,v| "city: #{k} , capital: #{v}" }
# city: Mexico , capital: Mexico city
# city: Colombia , capital: Bogota
# city: España , capital: Madrid
array4 = [["jonh",5],["brett",6],["ben",7]]
new_hash = array4.to_h # {"jonh"=>5, "brett"=>6, "ben"=>7}
new_hash.include? "brett" #true
new_hash["brett"] # 6
new_hash.select {|k,v| v.even?} # {"brett"=>6}
has2 = new_hash.map {|k,v| [k, v*2 ]}.to_h # {"jonh"=>10, "brett"=>12, "ben"=>14}
puts has2
#Ranges
(1..10) # inclusive range, from 1 to 10
(1...10) # exclusive range, from 1 to 9
(1..10).each {|x| puts x}
(1..10).map {|x| x*2}
(1..10).select {|x| x.odd? }
("a".."d").each {|x| puts x} # Range of letters
#Regex
is_gmail_regex = /\w+@gmail.com/
email = "jacob@gmail.com"
puts email.match is_gmail_regex
#Proc
saludar = Proc.new { |x| puts "Hola #{x}"}
saludar.call("Julio")
class Transaction
def exec
puts "Executing transaction"
if block_given?
yield
end
end
end
tx = Transaction.new
tx.exec
tx2 = Transaction.new
tx.exec { puts "Execution block given"}
# OOP
class Person
def initialize(name, age)
@name = name
@age = age
end
def name # getter
@name
self
end
def name=(name) # setter
@name = name
end
def self.suggestion_names # method of class | static class method, no related to a instance of this class
["Joseph","Kyle","Lenard"]
end
end
p1 = Person.new("Joshua", 3)
p1.name = "Josua Walton"
# p1.age = 5 # Error on this case, because , setter age is not defined
puts p1.name
puts Person.suggestion_names
# OOP - attr_accessor. attr_accessor is a macro on ruby
class Employee
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
def self.suggestion_names # method of class | static class method, no related to a instance of this class
["Joseph","Kyle","Lenard"]
end
end
p1 = Employee.new("Joshua", 3)
p1.name = "Josua Walton"
p1.age = 5
puts p1.name
puts Employee.suggestion_names
# Modules
module Models
class Person
def exec
puts "Models.Person.exec"
end
end
class Car
def run
puts "Models.Car.run"
end
end
end
module Reports
class Excel
def exec
puts "Reports.Excel.exec"
end
end
end
excel = Reports::Excel.new
excel.exec
# Boundle
# 1. Execute this command to create the Gemfile:
# bundle init
# 2. Inside Gemfile, I will find this site, there I can check several gems:
# https://rubygems.org/
# 2.1 Another recommended site:
# https://www.ruby-toolbox.com/
# 3. On https://rubygems.org/, look for 'faker', and copy its line to add on Gemfile.
# Write on Gemfile:
# gem 'faker', '~> 2.19'
# 4. Execute this command to install the gems defined on Gemfile
# bundle install
# 5. After the instalation, a Gemfile.lock is created, its a file that constains the current gems installed
# 6. On command line > irb , you can write:
require 'faker'
# and then explore:
# Faker::Movies::HarryPotter.methods
puts Faker::Movies::HarryPotter.spell
# 7. To ensure to use the gem defined on Gemfile, execute your program as: bundle exec ruby Exercises.rb
# Testing on Ruby
class Calculator
def sum(a,b)
a + b
end
def substract(a,b)
a - b
end
end
calc = Calculator.new
puts calc.sum(2,5)
# Manual test:
test = {[2,5]=>7, [2,1]=>3 }
test.each { |k,v|
if calc.sum(k[0],k[1]) != v
puts "Error"
end
}
# testing with Minitest
require 'minitest/autorun' # To run automaticly the test
class TestCalculator < Minitest::Test
def setup
@cal = Calculator.new
end
def test_positive_numbers_sum()
result = @cal.sum(3,4)
assert_equal result, 7
end
end