-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobj_14.rb
executable file
·63 lines (46 loc) · 967 Bytes
/
robj_14.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
#!/usr/bin/env ruby
require "./ruler"
testing = 1
#-----------------------------------------------------------------------------
R("")
a = []
for i in (0..10) do
val = (Random.rand() * 100).to_i
if testing then
val = i
end
a.push(i)
end
a.each do |av|
print "#{av.to_s.rjust(3)} "
end
puts ""
#-----------------------------------------------------------------------------
R("Now reinitialize to constant array");
a = [ 1, -2, 17, 4, 19, 19, 16, 21, 15 ]
b = a.select { |av| av if av % 2 == 0 }.flatten
puts b
#-----------------------------------------------------------------------------
R("class")
class Person
@age = 0
@name = ""
def initialize(age, name)
@age = age
@name = name
end
def mod_inc(person, n)
person.inc(n)
end
def to_s
"#{@name} age is #{@age}"
end
def inc(v)
@age += v
end
end
john = Person.new(10, "john")
mary = Person.new(20, "mary")
puts john, mary, "---"
mary.mod_inc(john, 2)
puts john, mary, "---"