-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobj_16.rb
executable file
·53 lines (42 loc) · 904 Bytes
/
robj_16.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
#!/usr/bin/env ruby
require "./ruler"
#-----------------------------------------------------------------------------
R("Showing how to handle to_s of the inherited class")
class Obj
attr_accessor :x, :y
def initialize(xx, yy)
@x, @y = xx, yy
end
def to_s
"x:#{@x} y:#{@y}"
end
end
class Planet < Obj
attr_accessor :color
def initialize(xx, yy, _color)
super(xx, yy)
@color = _color
end
def to_s
super + " color: #{@color}"
end
end
p0 = Planet.new(10, 20, "red")
p1 = Planet.new(30, 10, "red")
puts p0, p1
#-----------------------------------------------------------------------------
R("Use include=instance methods; extend==class methods")
module Mover
def move_to(x_to, y_to)
puts "x_to: #{x_to}, y_to: #{y_to}"
self.x += x_to
self.y += y_to
end
end
class Obj
include Mover
end
p3 = Planet.new(15, 25, "Planeta p3, color red")
puts p3
p3.move_to(10, 20)
puts p3