-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby_objects.html
More file actions
137 lines (129 loc) · 5.09 KB
/
ruby_objects.html
File metadata and controls
137 lines (129 loc) · 5.09 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
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
<html>
<head>
<title>Ruby objects notes</title>
</head>
<h1>Ruby notes: Objects</h1>
<h2>Objects etc...</h2>
<h3><strong>class keyword</h3>
'New up' a class with the <strong>class</strong> keyword.
<p>
<code>
class Song
<br/>
def initialize(artist, title, duration)
<br/>
@artist = artist
<br/>
@title = title
<br/>
@duration = duration
<br/>
end
<br/>
end
</p>
</code>
<h3>naming conventions</h3>
Class names start with a capital letter, methods lower case.
Ruby provides an <code>initialize</code> method, by default. This is similar to a constructor - it will receive constructor parameters. It can be overridden...
<p/><strong>Instance variables</strong> are denoted by '@'
<h3>inspect and to_s (to string)</h3>
<code>Inspect</code> gives us a string representation of any object; but the output isn't nicely formatted. Override <code>to_s</code> in the subclass providing some special formatting...
<h3>Class inheritance the < notation</h3>
<ul><li>Indicate object inheritance by using the < character.</li>
<li>Call the corresponding superclass bit via the <pre>super</pre> keyword</li>
<li>Use the keyword <monospace>super</monospace> to call up the object hierarchy</li>
<li>Ruby will allocate the class <pre>Object</pre> as the default supertype if a class does not explicitly declare one</li>
</ul>
For example,
<code><pre>class KaraokeSong < Song
def initialize(name, artist, duration, lyrics)
super(name, artist, duration)
@lyrics = lyrics
end
end</pre></code>
<h3>Inheritance and Mixins</h3>
Ruby does not provide multiple inheritance <i>per se</i> but it does support <pre>Mixins</pre>. Mixins allow functionality to be composed from different places, without incurring the ambiguity of multiple inheritance.
<b>A Mixin is like a partial class definition.</b>
<h3>Object attributes</h3>
We can return attribute values via accessor methods, e.g.
<code>
<pre>
class Song
def artist
@artist
end
def title
@title
end
def duration
@duration
end
end
</pre>
</code>
But because this is such a common idiom, Ruby provides an 'accessor methods' shorthand, wich looks like,
<code>
<pre>
class Song
attr_reader :artist, :title, :duration # this use of colon is a Symbol - the @ denoted instance variables/ attributes are created implicitly!!!!
end
</pre></code>
<b>A Symbol - that employs the colon, e.g. ':artist' denotes the variable name, while the variable name itself, e.g. 'artist' is the pointer to the actual value.
<h3>Attribute value assignment</h3>
Ruby uses setters for class attributes - similarly to getters shown above. It includes an idiosyncracy that if a method name includes an '=' sign in the title, it will be eligible for the left hand side of a value assignment, e.g.
<code><pre>
class Song
def duration=(newDuration)
@duration=newDuration
end
end
</pre></code>
As before, Ruby provides a shorthand, "attr_writer"
<code><pre>
class Song
attr_writer :duration
end
aSong = Song.new("Bicylops", "Fleck", 260)
aSong.duration = 257</pre></code>
<h3>Virtual Attributes</h3>
Methods that look like setters but actually perform a calculation - and return that value (but don't hold it as a field).
Here's a coded example - <i>note the methods have similar names - the signature that takes a parameter is called through the use of the equals sign</i>
<code><pre>
class Song
def durationInMinutes
@duration/60.0 # force floating point
end
def durationInMinutes=(value)
@duration = (value*60).to_i
end
end
aSong = Song.new("Bicylops", "Fleck", 260)
aSong.durationInMinutes » 4.333333333
aSong.durationInMinutes = 4.2
aSong.duration » 252
</pre></code>
<h3>Class Variables</h3>
Class variables are close to the Smalltalk definition of classes. The value held in a class variable is shared across all instances of that class - if it is set once in code, all other references to that class or object instances of that type will get access to that same shared value.
<ul><li>They must be initialized before use</li><li>They are NOT global or instance variables - whose value can be reassigned</li></ul>
<h3>Class Methods</h3>
Class methods (similar-ish to Java static methods) are declared by including the class name and period '.' in between the method name.
For example,
<code><pre>
Class Example
def instMeth # instance method
end
def Example.classMeth # class method
end
end
</pre></code>
<h3>Access Control</h3>
Access control is simlar to other OO languages and includes the modifiers,
<ol><li>private</li><li>protected</li><li>public</li></ol>
But! differs in that it is dynamic - and won't provoke compiler complaints (problems emerge at runtime).
Access modifiers can be used in two ways:
<ol><li>With no arguments: everything following the declaration will receive that same visibility</li><li>Provide declared methods as arguments to an access modifier and the methods receive that visibility.</li</ol>
A class's initialize method is automatically declared to be <code>private</code>.
<h3>Variables</h3>
...instance variables are pointers in Ruby like other OO languages
...prevent anyone from changing a particular object by freezing it