-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path07.rb
473 lines (359 loc) · 6.8 KB
/
07.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/usr/bin/ruby -w
def f(n)
puts n
return n
end
def s
puts 'ã'
end
#exit!
def at_exit
puts 'bye'
end
s
f(3+2) +1
puts Float::MAX
puts even = (20[0] == 0)
puts '-------------'
puts (0.4 - 0.3 == 0.1)
puts `ls`
puts '-------------'
4.times { puts :test.object_id }
puts '-------------'
greeting = "Hello"
greeting << " " << "World"
puts greeting
puts '-------------'
puts VERSION
puts `ruby -v`
puts '-------------'
s = 'hello'
s[0] = ?H
s[-1] = ?O
s[-1] = ""
s[1] = '&'
s[1..2] = 'oi'
puts s
puts '-------------'
s = "hellõ"
while(s["l"])
s["l"] = "L";
end
s['eL'] = '&'
s['õ'] = 'ã'
puts s
puts '-------------'
s = 'trocã primeira võgal por asterisco'
puts s.length
s[/[aeiouã]/] = '*'
s[/[ã]/] = 'a'
puts s
puts '-------------'
puts 'ãa'[2]
puts '-------------'
puts Array.new 4, 'xs'
puts '-------------'
a = []
a[2] = 2
puts a
puts '-------------'
puts s[1..-1]
puts '-------------'
a = []
a << 1
a << 2 << 3
a << [4,5,6]
p a
puts '-------------'
p a*3
puts '-------------'
a = [1, 1, 1, 1, 2, 2, 3]
p a | a # uniao de 'conjuntos'
p a & a # interseccao de 'conjuntos'
puts '-------------'
class RangeCrazy
attr_accessor :value
def initialize(value)
@value = value
end
def coerce(instance)
@value.coerce instance
end
def <=>(instance)
@value<=> instance
end
def succ
@value.succ
end
end
r = RangeCrazy.new(2)..RangeCrazy.new(4);
puts r.to_a
puts '-------------'
p :'simbolo com espaço'
p [].respond_to? :each
puts '-------------'
puts 'a' == "a"
puts String === 'b'
puts 'b'.hash
puts '-------------'
p 'any thing is a Object'.is_a? Object
p 'but not a instance of Object'.instance_of? Object
puts '-------------'
puts '=== é usado em CASE'
p (1..10) === 5
p (1..10) == 5
p /\d+/ === "123"
p String === "s"
p 's' === "s"
puts '-------------'
p 1.between?(0,10)
# true: 0 <= 1 <= 10
s = "ice"
s.freeze
s.frozen?
#s.upcase!
#s[0] = "ni"
class String
UMACONSTANTE = 'oi'
end
puts String::UMACONSTANTE
puts a2 = 'oi' unless a2
puts '-------------'
x, y, z = 1, *[2,3]
p x, y, z
puts '-------------'
class String
def -@
'-'
end
end
puts -"oi"
puts '-------------'
x = 'print'
x && puts(x.to_s)
puts x if x
puts '-------------'
(1..10).each {|x| print x if x==3..x==5 }
puts "\n"+'-------------'
puts case 2.0
when String then "string"
when Numeric then "number"
when TrueClass || FalseClass then "boolean"
else "other"
end
puts '-------------'
puts 'continue = NEXT'
def hasValue?(x)
case x
when [], "", 0, nil, false
false
else
true
end
end
puts hasValue? []
puts hasValue? 0
puts hasValue? ""
puts hasValue? nil
puts hasValue? false
puts hasValue? 'oi'
puts '-------------'
x = 0
puts x = x + 1 while x < 10
puts '-------------'
# Print the elements in an array
array = [1,2,3,4,5]
for element in array
puts element
end
# Print the keys and values in a hash
hash = {:a=>1, :b=>2, :c=>3}
for key,value in hash
puts "#{key} => #{value}"
end
puts '-------------'
0.step(Math::PI, 0.1) {|x| puts Math.sin(x) }
evens = (1..10).select {|x| x%2 == 0} # => [2,4,6,8,10]
odds = (1..10).reject {|x| x%2 == 0} # => [1,3,5,7,9]
puts '-------------'
def sequence(n, m, c)
i, s = 0, []
while i < n
y = m*i + c
yield y if block_given?
s << y
i += 1
end
s
end
puts (sequence 4, 2, 0.1)
puts '-------------'
s = "helloã"
s.enum_for(:each_char).map {|c| puts c.succ }
p s.each_byte.to_a
puts '-------------'
s = (1..10).select do |x|
next if x==6
x%2 == 0
end
puts s
puts '-------------'
1.upto(10) do |i|
1.upto(10) do |i|
print "#{i} "
end
print " ==> Row #{i}\n"
end
puts '-------------'
# return from find
def find(array, target)
array.each_with_index do |element,index|
return index if (element == target)
end
nil
end
# break
def find2(array, target)
value = array.each_with_index do |element,index|
break index if (element == target)
end
value unless value == array
#value unless value.is_a? Array
end
puts find [1,1,3,3,3,4,5], 3
puts find2 [1,1,3,3,3,4,5], 3
puts find2 [1,1,3,3,3,4,5], 6
puts '-------------'
puts caller 0
puts '-------------'
puts 'EXCEPTION:'
def factorial(n)
raise TypeError, "Integer argument expected" if not n.is_a? Integer
raise ArgumentError, 'bad argument' if n < 1
return 1 if n == 1
n * factorial(n-1)
end
#raise 'bad argument' if n < 1
#raise RuntimeError, "bad argument" if n < 1
#raise RuntimeError.new("bad argument") if n < 1
#raise RuntimeError.exception("bad argument") if n < 1
begin
puts factorial 0
puts factorial '0'
rescue => ex
puts "#{ex.class}: #{ex.message}"
p $!
rescue ArgumentError => ex
puts "Try again with a value >= 1"
rescue TypeError => ex
puts "Try again with an integer"
end
puts '-------------'
def explode
raise "bam!" if rand(10) == 0
end
def risky
begin
10.times do
explode
end
rescue TypeError
puts $!
end
"hello"
end
def defuse
begin
puts risky
rescue RuntimeError => e
puts e.message
end
end
defuse
puts '-------------'
def get
require 'open-uri'
tries = 0
begin
tries += 1
open('http://www.curecriativa.com/') {|f| puts f.readlines }
rescue OpenURI::HTTPError => e
puts e.message
if tries < 4
sleep(2**tries)
retry
end
end
end
puts '-------------'
def method_name(x)
# The body of the method goes here.
# Usually, the method body runs to completion without exceptions
# and returns to its caller normally.
rescue
# Exception-handling code goes here.
# If an exception is raised within the body of the method, or if
# one of the methods it calls raises an exception, then control
# jumps to this block.
else
# If no exceptions occur in the body of the method
# then the code in this clause is executed.
ensure
# The code in this clause is executed no matter what happens in the
# body of the method. It is run if the method runs to completion, if
# it throws an exception, or if it executes a return statement.
end
puts '-------------'
# Global hash for mapping line numbers (or symbols) to continuations
$lines = {}
# Create a continuation and map it to the specified line number
def line(symbol)
callcc {|c| $lines[symbol] = c }
end
# Look up the continuation associated with the number, and jump there
def goto(symbol)
$lines[symbol].call
end
# Now we can pretend we're programming in BASIC
i = 0
line 10
puts i += 1
goto 10 if i < 5
line 20
puts i -= 1
goto 20 if i > 0
puts '-------------'
o = "message"
def o.printme
puts self
end
o.printme
puts '-------------'
def sum(x,y); x+y; end
puts sum(1,2)
undef sum
# alias new_name existing_name
puts '-------------'
def suffix(s, index=s.size-1)
s[index, s.size-index]
end
p suffix 'fuck'
puts '-------------'
def max(max, *rest)
rest.each {|x| max = x if x > max }
max
end
p max(1,2,4,5,1,2,3,4)
data = [2,3,4,1,23,2,24]
p max *data
puts '-------------'
p max(*'hello world'.each_char)
puts '-------------'
def sequence3(n, m, c, &b)
i = 0
while(i < n)
b.call(i*m + c)
i += 1
end
end
sequence3 5, 2, 2, &lambda {|x| puts x }