-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathread.rb
More file actions
94 lines (71 loc) · 1.48 KB
/
Copy pathread.rb
File metadata and controls
94 lines (71 loc) · 1.48 KB
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
# Modules
# Encapsulates behaviour
module Area
PI = 3.1416
def self.square(n)
n * n
end
def self.rectangle(a,b)
a * b
end
def self.circle(r)
PI * r * r
end
end
Area.square(10) # 100
Area.rectangle(10,5) #50
Area.circle(5) # 78.54
# Classes
# Attributes: State
class Person
# initialize sets up attributes for a new object
def initialize(name, lastname)
@name = name
@lastname = lastname
end
end
# Create an object
kuri = Person.new('Abraham', 'Kuri')
puts kuri.inspect # #<Person:0x007fd6399e0540 @name="Abraham", @lastname="Kuri">
# Access attributes
# Manipulate state of the object
class Dog
attr_reader :age # Crea solo metodo de lectura
attr_writer :name # Crea solo metodo de escritura
attr_accessor :owner # Crea ambos metodos
def initialize()
@age = 0
end
end
# A dog is born
dog = Dog.new
# Access/Manipulation of attributes
dog.name = 'Fido'
dog.owner # => nil
dog.owner = kuri
dog.owner # => #<Person:0x007fd6399e0540>
# Instance Methods
# Behaviour of the object
class Cat
def kick
puts 'miaou [Please dont kick me]'
end
end
cat = Cat.new
cat.kick # => miaou [Please dont kick me]
# Class Methods
# Bahaviour of the class
class Horse
def initialize(name)
@name = name
end
def self.info
'Big Animal'
end
def info
"#{Horse.informacion} named #{@name}"
end
end
horse = Horse.new('London')
puts Horse.info # => Big Animal
puts horse.info # => Big Animal named London