-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruby-scripts
More file actions
228 lines (171 loc) · 4.76 KB
/
ruby-scripts
File metadata and controls
228 lines (171 loc) · 4.76 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
## RuBY
remplazar s con th
print "Thtring, pleathe!: "
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/, "th")
else
puts "Nothing to do here!"
end
puts "Your string is: #{user_input}"
## hash o diccionario
my_hash = { "name" => "Eric",
"age" => 26,
"hungry?" => true
}
puts my_hash["name"]
puts my_hash["age"]
puts my_hash["hungry?"]
## trabajando con diccionarios o hashes.
secret_identities = {
"The Batman" => "Bruce Wayne",
"Superman" => "Clark Kent",
"Wonder Woman" => "Diana Prince",
"Freakazoid" => "Dexter Douglas"
}
secret_identities.each do |item, name|
puts "#{item}: #{name}"
end
lunch_order = {
"Ryan" => "wonton soup",
"Eric" => "hamburger",
"Jimmy" => "sandwich",
"Sasha" => "salad",
"Cole" => "taco"
}
lunch_order.each {|element, food| puts "#{food}" }
### frecuencia de palabras.
puts "Introduzca sus palabras"
text = gets.chomp
words = text.split
frequencies = Hash.new(0)
words.each{ |word| frequencies[word] += 1 }
frequencies = frequencies.sort_by do |word, count|
count
end
frequencies.reverse!
frequencies.each{|word, frequency| puts word + " " + frequency.to_s}
# method that capitalizes a word
def capitalize(string)
puts "#{string[0].upcase}#{string[1..-1]}"
end
capitalize("ryan") # prints "Ryan"
capitalize("jane") # prints "Jane"
# block that capitalizes each string in the array
["ryan", "jane"].each {|string| puts "#{string[0].upcase}#{string[1..-1]}"} # prints "Ryan", then "Jane"
= areglando en orden alfabetico decendente
fruits = ["orange", "apple", "banana", "pear", "grapes"]
fruits.sort! do |firstFruit, secondFruit|
secondFruit <=> firstFruit
end
# benchmark
require 'benchmark'
string_AZ = Hash[("a".."z").to_a.zip((1..26).to_a)]
symbol_AZ = Hash[(:a..:z).to_a.zip((1..26).to_a)]
string_time = Benchmark.realtime do
100_000.times { string_AZ["r"] }
end
symbol_time = Benchmark.realtime do
100_000.times { symbol_AZ[:r] }
end
puts "String time: #{string_time} seconds."
puts "Symbol time: #{symbol_time} seconds."
## CRUD
movies = {
Memento: 3,
Primer: 4,
Ishtar: 1
}
puts "What would you like to do?"
puts "-- Type 'add' to add a movie."
puts "-- Type 'update' to update a movie."
puts "-- Type 'display' to display all movies."
puts "-- Type 'delete' to delete a movie."
choice = gets.chomp.downcase
case choice
when 'add'
puts "What movie do you want to add?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "What's the rating? (Type a number 0 to 4.)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}."
else
puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
end
when 'update'
puts "What movie do you want to update?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found!"
else
puts "What's the new rating? (Type a number 0 to 4.)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been updated with new rating of #{rating}."
end
when 'display'
movies.each do |movie, rating|
puts "#{movie}: #{rating}"
end
when 'delete'
puts "What movie do you want to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found!"
else
movies.delete(title.to_sym)
puts "#{title} has been removed."
end
else
puts "Sorry, I didn't understand you."
end
## solo if
puts 1 < 2 ? "One is less than two!": "One is not less than two."
= IF
puts "What's your favorite language?"
language = gets.chomp
if language == "Ruby"
puts "Ruby is great for web apps!"
elsif language == "Python"
puts "Python is great for science."
elsif language == "JavaScript"
puts "JavaScript makes websites awesome."
elsif language == "HTML"
puts "HTML is what websites are made of!"
elsif language == "CSS"
puts "CSS makes websites pretty."
else
puts "I don't know that language!"
end
puts "What's your favorite language?"
language = gets.chomp
case language
when "Ruby" then puts "Ruby is great for web apps!"
when "Python" then puts "Python is great for science."
when "JavaScript" then puts "JavaScript makes websites awesome."
when "HTML" then puts "HTML is what websites are made of!"
when "CSS" then puts "CSS makes websites pretty."
else
puts "I don't know that language!"
end
# codigo para imprimir un numero dado (n) de primos ruby 1.9.
require 'prime'
def first_n_primes(n)
# Check for correct input!
"n must be an integer" unless n.is_a? Integer
"n must be greater than 0" if n <= 0
prime = Prime.instance
prime.first n
end
first_n_primes(10)
# YIeld
def block_test
puts "We're in the method!"
puts "Yielding to the block..."
yield # aca va el bloque.
puts "We're back in the method!"
end
block_test { puts ">>> We're in the block!" }