You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ruby/lesson4/tutorial.md
+28-21Lines changed: 28 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,21 +5,21 @@ title: Object Oriented Ruby and Inheritance
5
5
6
6
# Before we start
7
7
8
-
There are no sample files for this tutorial as you should already know how to create a new Ruby file yourself. Before you begin, remember to create a new directory in your Github project or a new repository where you can store your exercises and practice code.
8
+
There are no sample files for this tutorial as you should already know how to create a new Ruby file yourself. Before you begin, remember to create a new directory in your Github project or a new repository where you can store your exercises and try out code.
9
9
10
-
Don't forget to commit to git regularly and also try to **type** out the examples as much as possible instead of copy pasting!
10
+
Don't forget to commit to git regularly and also try to **type** out the examples as much as possible instead of copy &pasting!
11
11
12
-
If you are going through the tutorial on your own time and need any help, [join the chat](https://gitter.im/codebar/tutorials) but first read our [Code of Conduct](http://codebar.io/code-of-conduct) as we do not tolerate any inappropriate behavior.
12
+
If you are going through the tutorial in your own time and need any help then [join the chatroom](https://gitter.im/codebar/tutorials), but first read our [Code of Conduct](http://codebar.io/code-of-conduct) as we will not tolerate any inappropriate behavior.
13
13
14
14
# What is Object Oriented Programming?
15
15
16
-
Object Oriented Programming is a way of programming where different concepts as represented as **objects** and different functionality of those objects is called **methods**. In Ruby, an object is defined by a **class** and everything is an object; Strings, Integers, even true and false.
16
+
Object Oriented Programming is a way of programming where concepts are represented as **objects**, and the functionality of those objects are called **methods**. In Ruby, an object is defined by a **class** and everything is an object; Strings, Integers, even true and false.
17
17
18
18
## What is a class?
19
19
20
-
A class is a way of representing an object. It enables us to package up data and ways of manipulating them.
20
+
A class is a way of representing an object, a blueprint for describing how it should look and behave. It enables us to package up data, and define the ways in which it can be changed.
21
21
22
-
To define a class we need to specify the keyword **class** followed by the name we want to name our class, and the keyword **end** to close the definition.
22
+
To define a class we need to specify the keyword **class**, followed by the name we want our class to be known by, then the keyword **end** to close the definition.
23
23
24
24
```ruby
25
25
# defining a Color class
@@ -30,31 +30,38 @@ end
30
30
31
31
The name must be defined in camel case. **CamcelCase** is a way of writing compound words or phrases such that each word begins with a capital letter e.g. ColorCode, EmailAddress etc.
32
32
33
-
## Defining a class
33
+
## Creating an instance of a class
34
34
35
-
To create a new instance of an object, we use the keyword **new**. To specify parameters when defining a new class we can do so by defining a **constructor**. In Ruby, a constructor is specified using the `initialize` method.
35
+
To create an object, we call the **new** method on its class. If you want to pass information to the object, then you can write an `initialize` method, which Ruby will call whenever you call the classes' `new` method. Any parameters defined for the `initialize` method need to passed to `new`.
36
+
37
+
The `initialize` method is often called a **contructor**, as it helps *construct* new objects.
36
38
37
39
```ruby
38
40
classColor
39
41
40
42
definitialize(name, hexcode)
41
-
@name=name
42
-
@hexcode=hexcode
43
+
# Do cool things with name and hexcode
44
+
puts"The color #{name} has hexcode#{hexcode}"
43
45
end
44
46
end
45
47
46
48
color1 =Color.new("purple", "#8824a4")
47
49
color2 =Color.new("blue", "#4c6fcc")
48
50
```
49
51
50
-
## Variables and methods
52
+
Sometimes you'll hear objects referred to as **instances** of a particular class. This just means that the class was used as the blueprint to create the object.
53
+
54
+
## Instance variables and methods
51
55
52
-
To define attributes for a class, we use instance variables. Instance variable are defined using the **@**operator. They cannot be accessed outside the class unless we expose them through methods.
56
+
To define attributes for a class, we use instance variables. Instance variables are defined and used in the same way as normal variables, but their name must start with the **@**symbol, and they cannot be accessed outside of the object unless exposed via methods.
53
57
54
58
```ruby
55
59
classColor
56
60
57
-
...
61
+
definitialize(name, hexcode)
62
+
@name= name
63
+
@hexcode= hexcode
64
+
end
58
65
59
66
defname
60
67
@name
@@ -69,7 +76,7 @@ color = Color.new("purple", "#8824a4")
69
76
puts"The hexcode of #{color.name} is #{color.hexcode}"
70
77
```
71
78
72
-
We can also define the value of different attributes via methods (called setter methods)
79
+
We can also change the values of attributes via methods (often called setter methods):
73
80
74
81
```ruby
75
82
classColor
@@ -89,7 +96,7 @@ puts "The hexcode of #{color.name} is #{color.hexcode}"
89
96
90
97
## Privacy
91
98
92
-
By default, any method you define in Ruby (and most other languages too) is public. This means that anyone can call any of its methods with the exception of `initialize` which is always private and can be accessed through calling`new`.
99
+
By default, any method you define in Ruby (and most other languages too) is public. This means that anything outside of the object can call any of the methods, with the exception of `initialize` which is always private and can only be called by`new`.
93
100
94
101
Besides **public** you can also have **private** and **protected** methods.
95
102
@@ -121,7 +128,7 @@ Let's now put into practise what we have learned so far.
121
128
122
129
# Exercise 1: Celsius to Fahrenheit
123
130
124
-
Create a Celsius class, that takes as a parameter the temperature.
131
+
Create a Celsius class, that takes the temperature as a parameter.
125
132
126
133
> Remember to use the `initialize` method
127
134
@@ -158,7 +165,7 @@ In this exercise we want to create an object for the codebar tutorials. Each obj
158
165
- type
159
166
- difficulty
160
167
161
-
The difficulty of each tutorial must be defined by a symbol, and the available values are :easy, :medium, :hard, :advanced and :expert.
168
+
The difficulty of each tutorial must be defined by a symbol, and the available values are `:easy`, `:medium`, `:hard`, `:advanced` and `:expert`.
162
169
163
170
In the `Tutorial` class create a method called `is_harder_than?` that takes in another `tutorial` as a parameter and returns `true` if parent tutorial's difficulty level is higher than the tutorial passed in a parameter and `false` if the difficulty level is lower. This only happens if the tutorial types are the same. Alternatively, it outputs `You cannot compare a Ruby with a JavaScript tutorial` where _Ruby_ and _JavaScript_ are the types of the given tutorials.
Inheritance is a way for a class to get features from another parent class. It can make creating a program much easier to implement when there is common shared functionality between different objects.
196
+
Inheritance is a way for a class to get features from another parent class. It can make creating a program much easier to implement when functionality needs to be shared between objects.
190
197
191
-
In the example below, we create a Person class and a SuperHero class. As not all people have special powers, we can use SuperHero for those with special powers and Person for anyone else.
198
+
In the example below, we create a `Person` class and a `SuperHero` class. As not all people have superpowers, we can use `SuperHero` for those with superpowers and Person for anyone else.
192
199
193
200
```ruby
194
201
classPerson
@@ -218,7 +225,7 @@ end
218
225
219
226
### is_a?(Class)
220
227
221
-
With [`is_a?`](http://ruby-doc.org/core-2.1.1/Object.html#method-i-is_a-3F) you can check if the class or one of the superclasses (base) of an object is of a specific type.
228
+
With [`is_a?`](http://ruby-doc.org/core-2.1.1/Object.html#method-i-is_a-3F) you can check if the class or one of the superclasses (base) of an object.
222
229
Try this out. Before running the code in the example below, discuss it with your coach. What do you expect the result to be?
223
230
224
231
```ruby
@@ -234,7 +241,7 @@ jean_grey.is_a?(SuperHero)
234
241
235
242
### Overriding methods
236
243
237
-
By overriding methods, you can change the behavior of a method defined in the Superclass. For our superheroes example, we want the `to_s` method to return a different String than its base class including the super power.
244
+
By overriding methods, you can change the behavior of a method defined in the superclass. For our superheroes example, we want the `to_s` method to return a different `String` than its base class, including the superpower.
0 commit comments