-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself_class.rb
62 lines (50 loc) · 875 Bytes
/
self_class.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
#class << Hello
# def hello(name)
# puts "#{name} said hello"
# end
#end
#Hello.hello("Jhon")
#class HelloWorld
# class << self
# def hello(name)
# puts "#{name}"
# end
# end
#end
#HelloWorld.hello("haha")
=begin
class Point
attr_accessor :x, :y
protected :x=, :y= # ss
def initialize(x=0.0, y=0.0)
@x, @y = x, y
end
def swap(other)
tmp_x, tmp_y = @x, @y
@x, @y = other.x, other.y
other.x, other.y = tmp_x, tmp_y
return self
end
end
p0 = Point.new
p1 = Point.new(1.0, 2.0)
p [ p0.x, p0.y ]
p [ p1.x, p1.y ]
p0.swap(p1)
p [ p0.x, p0.y ]
p [ p1.x, p1.y ]
=end
=begin
module HelloModule
Version = "1.0"
def hello(name)
puts "Hello,#{name}"
end
module_function :hello
end
p HelloModule::Version
HelloModule.hello("Alice")
include HelloModule
p Version
hello("Alice")
=end