-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathruby_oo.html
More file actions
381 lines (346 loc) · 21.2 KB
/
ruby_oo.html
File metadata and controls
381 lines (346 loc) · 21.2 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<!doctype html>
<html dir="ltr" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ruby Object Orientation — Ruby on Rails Guides</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbo-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/highlight.css" data-turbo-track="reload">
<link rel="icon" href="images/backend-development.svg" sizes="any">
<script src="javascripts/@hotwired--turbo.js" data-turbo-track="reload"></script>
<script src="javascripts/clipboard.js" data-turbo-track="reload"></script>
<script src="javascripts/guides.js" data-turbo-track="reload"></script>
<meta property="og:title" content="Ruby Object Orientation — Ruby on Rails Guides" />
<meta name="description" content="Ruby Object OrientationThis guide will take a closer look at objects, classes and modules in Ruby.After finishing this guide you will know how to write object methods and use object variables (properties) know how to write class methods and use class variables (properties)" />
<meta property="og:description" content="Ruby Object OrientationThis guide will take a closer look at objects, classes and modules in Ruby.After finishing this guide you will know how to write object methods and use object variables (properties) know how to write class methods and use class variables (properties)" />
<meta property="og:locale" content="en_US" />
<meta property="og:site_name" content="Textbook Backend Developemnt" />
<meta property="og:image" content="images/backend-development.svg" />
<meta property="og:type" content="website" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Heebo:wght@100..900&family=Noto+Sans+Arabic:wght@100..900&display=swap" rel="stylesheet">
<meta name="theme-color" content="#2e56e9">
</head>
<body class="guide">
<header id="page_header">
<div class="wrapper clearfix">
<nav id="feature_nav">
<div class="header-logo">
<a href="/">Backend Development</a>
</div>
<ul class="nav">
<li><a class="nav-item" id="home_nav" href="/">Home</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Index</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="guides-section-container">
<div class="guides-section">
<dt>Ruby on Rails</dt>
<dd><a href="ruby_commandline.html">Ruby Commandline</a></dd>
<dd><a href="rails_database_and_model.html">Models and ActiveRecord</a></dd>
<dd><a href="rails_db.html">Database and Migrations</a></dd>
<dd><a href="rails_associations_and_validations.html">Associations and Validations</a></dd>
<dd><a href="rails_view_and_controller.html">Routing, View and Controller</a></dd>
<dd><a href="rails_authentication.html">Simple Authentication</a></dd>
<dd><a href="assets_and_import_map.html">The Asset Pipeline and Importmaps</a></dd>
<dd><a href="testing.html">Getting started with Testing</a></dd>
<dd><a href="refactoring_rails.html">Refactoring Rails</a></dd>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rails_gems.html">Ruby Gems for your Rails Project</a></dd>
<dd><a href="deploying_rails.html">Deploying Rails</a></dd>
</div>
<div class="guides-section">
<dt>Ruby on Rails - Advanced Topics</dt>
<dd><a href="deploy-to-paas.html">Deploy to PAAS</a></dd>
<dd><a href="rest-api.html">REST API</a></dd>
<dd><a href="graphql-api.html">GraphQL API</a></dd>
<dd><a href="rails_websockets.html">Websocket in Rails</a></dd>
<dd><a href="jobs_and_tasks.html">Jobs and Tasks in Rails</a></dd>
<dd><a href="rails_security.html">Rails Security</a></dd>
</div>
<div class="guides-section">
<dt>Overarching Concerns</dt>
<dd><a href="issue.html">Issue Lifecycle</a></dd>
<dd><a href="security.html">Security</a></dd>
<dd><a href="adv_authentication.html">Advanced Authentication</a></dd>
<dd><a href="caching.html">Caching</a></dd>
<dd><a href="advanced_testing.html">Advanced Testing</a></dd>
<dd><a href="internationalization.html">Internationalization (I18n)</a></dd>
<dd><a href="git_rebasing.html">Git Rebasing</a></dd>
</div>
<div class="guides-section">
<dt>Nodes.js</dt>
<dd><a href="node_vs_rails.html">Node vs. Rails</a></dd>
<dd><a href="node_basics.html">Node Basics</a></dd>
<dd><a href="node_websockets.html">Node Websockets</a></dd>
<dd><a href="node_express.html">Node Web App</a></dd>
<dd><a href="node_cluster.html">Scaling Node</a></dd>
</div>
<div class="guides-section">
<dt>Next.js</dt>
<dd><a href="nextjs.html">Next.js</a></dd>
</div>
</dl>
</div>
</li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">Index</option>
<optgroup label="Ruby on Rails">
<option value="ruby_commandline.html">Ruby Commandline</option>
<option value="rails_database_and_model.html">Models and ActiveRecord</option>
<option value="rails_db.html">Database and Migrations</option>
<option value="rails_associations_and_validations.html">Associations and Validations</option>
<option value="rails_view_and_controller.html">Routing, View and Controller</option>
<option value="rails_authentication.html">Simple Authentication</option>
<option value="assets_and_import_map.html">The Asset Pipeline and Importmaps</option>
<option value="testing.html">Getting started with Testing</option>
<option value="refactoring_rails.html">Refactoring Rails</option>
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rails_gems.html">Ruby Gems for your Rails Project</option>
<option value="deploying_rails.html">Deploying Rails</option>
</optgroup>
<optgroup label="Ruby on Rails - Advanced Topics">
<option value="deploy-to-paas.html">Deploy to PAAS</option>
<option value="rest-api.html">REST API</option>
<option value="graphql-api.html">GraphQL API</option>
<option value="rails_websockets.html">Websocket in Rails</option>
<option value="jobs_and_tasks.html">Jobs and Tasks in Rails</option>
<option value="rails_security.html">Rails Security</option>
</optgroup>
<optgroup label="Overarching Concerns">
<option value="issue.html">Issue Lifecycle</option>
<option value="security.html">Security</option>
<option value="adv_authentication.html">Advanced Authentication</option>
<option value="caching.html">Caching</option>
<option value="advanced_testing.html">Advanced Testing</option>
<option value="internationalization.html">Internationalization (I18n)</option>
<option value="git_rebasing.html">Git Rebasing</option>
</optgroup>
<optgroup label="Nodes.js">
<option value="node_vs_rails.html">Node vs. Rails</option>
<option value="node_basics.html">Node Basics</option>
<option value="node_websockets.html">Node Websockets</option>
<option value="node_express.html">Node Web App</option>
<option value="node_cluster.html">Scaling Node</option>
</optgroup>
<optgroup label="Next.js">
<option value="nextjs.html">Next.js</option>
</optgroup>
</select>
</li>
</ul>
</nav>
</div>
</header>
<hr class="hide" />
<section id="feature">
<div class="wrapper">
<h1>Ruby Object Orientation</h1><p>This guide will take a closer look at objects, classes and modules in Ruby.</p><p>After finishing this guide you will</p>
<ul>
<li>know how to write object methods and use object variables (properties)</li>
<li>know how to write class methods and use class variables (properties)</li>
</ul>
<nav id="subCol">
<h3 class="chapter">
<picture>
<!-- Using the `source` HTML tag to set the dark theme image -->
<source
srcset="images/icon_book-close-bookmark-1-wht.svg"
media="(prefers-color-scheme: dark)"
/>
<img src="images/icon_book-close-bookmark-1.svg" alt="Chapter Icon" />
</picture>
Chapters
</h3>
<ol class="chapters">
<li><a href="#to-be-written">To Be Written</a></li>
<li><a href="#an-investigation-into-declarations">An investigation into Declarations:</a>
<ul>
<li><a href="#online-resources">Online Resources</a></li>
<li><a href="#books">Books</a></li>
</ul></li>
</ol>
</nav>
<hr>
</div>
</section>
<main id="container">
<div class="wrapper">
<div id="mainCol">
<div class='slide'>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-0' href='slides_ruby_oo.html#/0'>◻</a></p>
<h2 id="to-be-written"><a class="anchorlink" href="#to-be-written"><span>1</span> To Be Written</a></h2></div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-1' href='slides_ruby_oo.html#/1'>◻</a></p>
<h2 id="an-investigation-into-declarations"><a class="anchorlink" href="#an-investigation-into-declarations"><span>2</span> An investigation into Declarations:</a></h2><p>In <a href="rails_associations_and_validations.html">Rails: Associations and Validations</a> we learned that Validation are declared in the model:</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">Tweet</span> <span class="o"><</span> <span class="no">ApplicationRecord</span>
<span class="n">validates_presence_of</span> <span class="ss">:status</span>
<span class="k">end</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="class Tweet < ApplicationRecord
validates_presence_of :status
end
">Copy</button>
</div>
<p>But what is really happening here?</p><p><code>validates_presence_of</code> is called when the class is defined. it is called only once.</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">Ruby</span>
<span class="k">def</span> <span class="nf">validates_presence_of</span> <span class="n">x</span>
<span class="nb">puts</span> <span class="s2">"I should really check for property </span><span class="si">#{</span><span class="n">x</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="n">validates_presence_of</span> <span class="ss">:color</span>
<span class="k">end</span>
<span class="c1"># in `<class:Ruby>': undefined method `validates_presence_of' for Ruby:Class (NoMethodError)</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="class Ruby
def validates_presence_of x
puts "I should really check for property #{x}"
end
validates_presence_of :color
end
# in `<class:Ruby>': undefined method `validates_presence_of' for Ruby:Class (NoMethodError)
">Copy</button>
</div>
<p><code>validates_presence_of</code> cannot be a normal method, because such a method is not defined yet
when the class is defined. so it needs to be a class method.</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">Ruby</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">validates_presence_of</span> <span class="n">x</span>
<span class="nb">puts</span> <span class="s2">"I should really check for property </span><span class="si">#{</span><span class="n">x</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="n">validates_presence_of</span> <span class="ss">:color</span>
<span class="k">end</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="class Ruby
def self.validates_presence_of x
puts "I should really check for property #{x}"
end
validates_presence_of :color
end
">Copy</button>
</div>
<p>....</p><div class="interstitial code">
<pre><code class="highlight ruby"><span class="k">class</span> <span class="nc">ValidatableStuff</span>
<span class="nb">attr_accessor</span> <span class="ss">:data</span>
<span class="nb">attr_accessor</span> <span class="ss">:errors</span>
<span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">validates_presence_of</span> <span class="n">x</span>
<span class="nb">puts</span> <span class="s2">"the class </span><span class="si">#{</span><span class="nb">self</span><span class="si">}</span><span class="s2"> should really check for </span><span class="si">#{</span><span class="n">x</span><span class="si">}</span><span class="s2"> - but how?"</span>
<span class="vc">@@validator_for</span> <span class="o">=</span> <span class="n">x</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="o">**</span><span class="n">data</span><span class="p">)</span>
<span class="vi">@data</span> <span class="o">=</span> <span class="n">data</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">save</span>
<span class="nb">puts</span> <span class="s2">"I am trying to save the data of a </span><span class="si">#{</span><span class="nb">self</span><span class="p">.</span><span class="nf">class</span><span class="si">}</span><span class="s2"> Object. the data is </span><span class="si">#{</span><span class="nb">self</span><span class="p">.</span><span class="nf">data</span><span class="si">}</span><span class="s2">"</span>
<span class="k">if</span> <span class="nb">self</span><span class="p">.</span><span class="nf">data</span><span class="p">.</span><span class="nf">keys</span><span class="p">.</span><span class="nf">include?</span><span class="p">(</span> <span class="vc">@@validator_for</span> <span class="p">)</span>
<span class="nb">puts</span> <span class="s2">"this is ok, </span><span class="si">#{</span><span class="vc">@@validator_for</span><span class="si">}</span><span class="s2"> is set"</span>
<span class="k">return</span> <span class="kp">true</span>
<span class="k">else</span>
<span class="nb">puts</span> <span class="s2">"this is not ok, </span><span class="si">#{</span><span class="vc">@@validator_for</span><span class="si">}</span><span class="s2"> needs to be set"</span>
<span class="nb">self</span><span class="p">.</span><span class="nf">errors</span> <span class="o">=</span> <span class="s2">"</span><span class="si">#{</span><span class="vc">@@validator_for</span><span class="si">}</span><span class="s2"> needs to be set"</span>
<span class="k">return</span> <span class="kp">false</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Ruby</span> <span class="o"><</span> <span class="no">ValidatableStuff</span>
<span class="k">def</span> <span class="nf">x</span>
<span class="k">end</span>
<span class="n">x</span>
<span class="n">validates_presence_of</span> <span class="ss">:color</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Diamond</span> <span class="o"><</span> <span class="no">ValidatableStuff</span>
<span class="n">validates_presence_of</span> <span class="ss">:carat</span>
<span class="k">end</span>
<span class="n">r1</span> <span class="o">=</span> <span class="no">Ruby</span><span class="p">.</span><span class="nf">new</span>
<span class="k">if</span> <span class="n">r1</span><span class="p">.</span><span class="nf">save</span>
<span class="nb">puts</span> <span class="s2">"saved"</span>
<span class="k">else</span>
<span class="nb">puts</span> <span class="s2">"errors: </span><span class="si">#{</span><span class="n">r1</span><span class="p">.</span><span class="nf">errors</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>
<span class="n">r2</span> <span class="o">=</span> <span class="no">Ruby</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">color: </span><span class="s1">'yellow'</span><span class="p">)</span>
<span class="n">r2</span><span class="p">.</span><span class="nf">save</span>
<span class="n">d1</span> <span class="o">=</span> <span class="no">Diamond</span><span class="p">.</span><span class="nf">new</span>
<span class="n">d1</span><span class="p">.</span><span class="nf">save</span>
<span class="n">d2</span> <span class="o">=</span> <span class="no">Diamond</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">carat: </span><span class="mi">10</span><span class="p">)</span>
<span class="n">d2</span><span class="p">.</span><span class="nf">save</span>
</code></pre>
<button class="clipboard-button" data-clipboard-text="class ValidatableStuff
attr_accessor :data
attr_accessor :errors
def self.validates_presence_of x
puts "the class #{self} should really check for #{x} - but how?"
@@validator_for = x
end
def initialize(**data)
@data = data
end
def save
puts "I am trying to save the data of a #{self.class} Object. the data is #{self.data}"
if self.data.keys.include?( @@validator_for )
puts "this is ok, #{@@validator_for} is set"
return true
else
puts "this is not ok, #{@@validator_for} needs to be set"
self.errors = "#{@@validator_for} needs to be set"
return false
end
end
end
class Ruby < ValidatableStuff
def x
end
x
validates_presence_of :color
end
class Diamond < ValidatableStuff
validates_presence_of :carat
end
r1 = Ruby.new
if r1.save
puts "saved"
else
puts "errors: #{r1.errors}"
end
r2 = Ruby.new(color: 'yellow')
r2.save
d1 = Diamond.new
d1.save
d2 = Diamond.new(carat: 10)
d2.save
">Copy</button>
</div>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-2' href='slides_ruby_oo.html#/2'>◻</a></p>
<h3 id="online-resources"><a class="anchorlink" href="#online-resources"><span>2.1</span> Online Resources</a></h3>
<ul>
<li><a href="http://rubylearning.com/satishtalim/tutorial.html">learningruby.com tutorial</a></li>
</ul>
</div>
<div class='slide'>
<p class='slide_break_block'><a class='slide_break' id='slide-3' href='slides_ruby_oo.html#/3'>◻</a></p>
<h3 id="books"><a class="anchorlink" href="#books"><span>2.2</span> Books</a></h3>
<ul>
<li><a href="https://www.poodr.com/">Metz, Sandy(2017): Practical Object-Oriented Design in Ruby</a></li>
</ul>
</div>
</div>
</div>
</main>
<hr class="hide" />
<footer id="page_footer">
<div class="wrapper">
<p class="copyright">published under <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/at/deed.de">creative commons by-nc-sa</a> in 2012-2025 by <a href="https://brigitte-jellinek.at">Brigitte Jellinek</a>.
</p>
<p>If you want to contribute: <a href="https://github.com/backend-development/backend-development-textbook/fork">fork the source on github</a>
</p>
<p>Favicon "backend development" by Arkinasi from <a href="https://thenounproject.com/browse/icons/term/backend-development/" target="_blank" title="backend development Icons">Noun Project</a> (CC BY 3.0)</p>
</div>
</footer>
</body>
</html>