Skip to content

Commit 7d0c15f

Browse files
committed
Merge pull request codebar#65 from BRMatt/patch-1
A couple of corrections to lesson 4
2 parents 35789d5 + 674d8bd commit 7d0c15f

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

ruby/lesson4/tutorial.md

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ title: Object Oriented Ruby and Inheritance
55

66
# Before we start
77

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.
99

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!
1111

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.
1313

1414
# What is Object Oriented Programming?
1515

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.
1717

1818
## What is a class?
1919

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.
2121

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.
2323

2424
```ruby
2525
# defining a Color class
@@ -30,31 +30,38 @@ end
3030

3131
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.
3232

33-
## Defining a class
33+
## Creating an instance of a class
3434

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.
3638

3739
```ruby
3840
class Color
3941

4042
def initialize(name, hexcode)
41-
@name = name
42-
@hexcode = hexcode
43+
# Do cool things with name and hexcode
44+
puts "The color #{name} has hexcode #{hexcode}"
4345
end
4446
end
4547

4648
color1 = Color.new("purple", "#8824a4")
4749
color2 = Color.new("blue", "#4c6fcc")
4850
```
4951

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
5155

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.
5357

5458
```ruby
5559
class Color
5660

57-
...
61+
def initialize(name, hexcode)
62+
@name = name
63+
@hexcode = hexcode
64+
end
5865

5966
def name
6067
@name
@@ -69,7 +76,7 @@ color = Color.new("purple", "#8824a4")
6976
puts "The hexcode of #{color.name} is #{color.hexcode}"
7077
```
7178

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):
7380

7481
```ruby
7582
class Color
@@ -89,7 +96,7 @@ puts "The hexcode of #{color.name} is #{color.hexcode}"
8996

9097
## Privacy
9198

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`.
93100

94101
Besides **public** you can also have **private** and **protected** methods.
95102

@@ -121,7 +128,7 @@ Let's now put into practise what we have learned so far.
121128

122129
# Exercise 1: Celsius to Fahrenheit
123130

124-
Create a Celsius class, that takes as a parameter the temperature.
131+
Create a Celsius class, that takes the temperature as a parameter.
125132

126133
> Remember to use the `initialize` method
127134
@@ -158,7 +165,7 @@ In this exercise we want to create an object for the codebar tutorials. Each obj
158165
- type
159166
- difficulty
160167

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`.
162169

163170
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.
164171

@@ -186,9 +193,9 @@ tutorial3.is_harder_than?(tutorial2)
186193
187194
## Inheritance
188195

189-
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.
190197

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.
192199

193200
```ruby
194201
class Person
@@ -218,7 +225,7 @@ end
218225

219226
### is_a?(Class)
220227

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.
222229
Try this out. Before running the code in the example below, discuss it with your coach. What do you expect the result to be?
223230

224231
```ruby
@@ -234,7 +241,7 @@ jean_grey.is_a?(SuperHero)
234241

235242
### Overriding methods
236243

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.
238245

239246
```ruby
240247
def to_s

0 commit comments

Comments
 (0)