-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path04.rb
357 lines (255 loc) · 4.2 KB
/
04.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
#!/usr/bin/ruby -w
puts 'o'
print 'numero: '
puts $.
puts $<
puts $>
puts $_
puts '-------------'
puts $$
puts $:
puts '-------------'
puts $"
puts $LOADED_FEATURES
puts $DEBUG
puts $FILENAME
puts $KCODE, $LOAD_PATH
puts '-------------'
puts __LINE__
puts (-123.class)
puts 0xFFFFFF.class
puts 010
puts 0b11
puts ?a
puts 102.chr
puts "a6" << 102
puts "the error is #{1.0/2}"
puts %(fghfgh 78% asdas)
puts 'opaX'.match %r[a.]
puts %s(oi d)
puts %s[oi hg u]
puts '-------------'
puts %w[essa \""e''h massa #{1+2}]
puts :OK
puts %W[essa \""eh massa #{1+1}]
puts '-------------'
print [<<END, "short", "strings\n\n"]
1
2
3
END
print [<<ONE, <<TWO, <<THREE]
the first thing
ONE
the second thing
TWO
and the third thing #{3+9}
THREE
puts '-------------'
puts %W'a b c #{2+1}'
## START, COUNT
puts ['a', 'b', 'c', 'd', 'e', 'f'][-4,10][1..3]
puts '-------------'
puts 'olha o split'.split.join '-,-'
puts (Array.new 4).fill('preenche tudo igual')
h = {:a => 1, :b => 2, :c => 3}
puts h
if (0..4) === 3
puts 'yep'
end
a = ['a', 'b', 'c']
puts *a
myArray = %w(John Michel Fran Adolf)
var1, var2, var3, var4 =* myArray
puts var1
puts '-------------'
puts false || 'o'
f = false
f ||= 'agora sim'
f ||= 'agora nao'
puts f
puts '-------------'
y = 'vai mostrar isso'
y = "(some fallback value)" unless y
puts y
if not nil
puts 'eras'
end
class Rt
def mt
return b = 'o' if false
return 'vale esse'
end
end
rt = Rt.new
puts rt.mt
puts '-------------'
if false
bunda = 'coisa'
end
p bunda
bunda &&= 'ã'
puts bunda
bunda ||= 'ã'
puts bunda
bunda &&= 'o'
puts bunda
puts '-------------'
4.times do |$global|
$global = $global*2
end
puts $global
puts '-------------'
class Pot2
def initialize
@value = 1
end
def go
@value *= 2
self
end
def to_s
@value.to_s
end
end
class Pot2s < Pot2
@@value = 1
def go
@@value *= 2
self
end
def to_s
@value = @@value
super
end
end
class Pot2S < Pot2
@@value = 1
def initialize
@value = @@value
end
def go
super
@@value = @value
self
end
end
puts (1..10).inject(Pot2.new) { |obj, i| obj.send(:go) }
puts (1..10).inject(Pot2s.new) { |obj, i| obj.go }
puts (1..10).inject(Pot2s.new) { |obj, i| obj.go }
puts (1..10).inject(Pot2S.new) { |obj, i| obj.go }
puts (1..10).inject(Pot2S.new) { |obj, i| obj.go }
@a = 2
puts @a
puts '-------------'
$go = :go
class Teste
if $go == :go
def self.foo
3
end
else
def self.foo
4
end
end
end
puts Teste.foo
puts '-------------'
a = 25
b = proc do
a
end
puts b.call
puts '-------------'
class TesteEscopo
a = 3
b = :go
define_method b do
a
end
define_method :succ do
a += 1
self
end
end
puts TesteEscopo.new.succ.succ.go
puts '-------------'
a = 3
PROC = proc { a }
class A
define_method :go, PROC
end
puts A.new.go
a = 4
puts A.new.go
if 0
puts 'zero é verdadeiro'
end
puts '* conditionals are expression'
a = if 0 then 2 else 3 end
puts a
puts true ? 't' : 'f'
a = 1
case
when a < 5 then puts "#{a} less than 5"
when a == 5 then puts "#{a} equals 5"
when a > 5 then puts "#{a} greater than 5"
end
while a < 5
a += 1
puts a
end
puts (1..10).each { |i|
break i if i > 5
}
puts '-------------'
def calculate_value(x, y, *rest)
puts rest
end
calculate_value(1, 2,'a','b','c')
calculate_value(*[1, 2,'a2','b2','c2'])
puts '-------------'
def foo_hash(var)
puts var.inspect
end
foo_hash :arg1 => 1, :arg2 => 2, :arg3 => 3
puts '-------------'
def method_call
yield
end
puts method_call(&PROC)
puts '-------------'
def gen_times(factor)
Proc.new {|n| n*factor}
end
puts gen_times(3).call(12)
puts '-------------'
def foo(a, b)
a.call b
end
foo(Proc.new{|x| puts x}, 34)
foo(lambda{|x| puts x+1}, 34)
puts '-------------'
def lamba_proc
(lambda{ return "Baaam1" }).call
'oi'
Proc.new{ |n| return n }["Baaam2"]
'oi'
'not printed'
end
puts lamba_proc
puts '-------------'
def contrived(a, &f)
f[a]
yield a
end
contrived(25) {|x| puts x}
def contrived2(a)
yield a
end
contrived(25, &lambda{|x| puts x})
puts '-------------'
calc = lambda{|n| n*2 }
puts "number: "+((1..10).collect(&calc).join(", "))
puts '-------------'