Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion concepts/arrays/about.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# About

Data structures that can hold zero or more elements are known as _collections_. An **array** in Ruby is a collection that maintains the ordering in which its objects are added. Arrays can hold any object. Objects can be added to an array or retrieved from it using an index. Ruby array indexing is zero-based, meaning that the first element's index is always zero:
Data structures that can hold zero or more elements are known as _collections_.
An **array** in Ruby is a collection that maintains the ordering in which its objects are added.
Arrays can hold any object.
Objects can be added to an array or retrieved from it using an index.
Ruby array indexing is zero-based, meaning that the first element's index is always zero:

```ruby
# Declare an array containing two values
Expand Down
19 changes: 12 additions & 7 deletions concepts/arrays/introduction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Introduction

In Ruby, **arrays** are ordered, integer-indexed collections of any object. Array indexing starts at `0`. A negative index is assumed to be relative to the end of the array — i.e. an index of `-1` indicates the last element of the array, `-2` is the next to last element in the array, and so on.
In Ruby, **arrays** are ordered, integer-indexed collections of any object.
Array indexing starts at `0`.
A negative index is assumed to be relative to the end of the array — i.e., an index of `-1` indicates the last element of the array, `-2` is the next to last element in the array, and so on.
Ruby arrays mix in the [Enumerable module][enumerable-module], which adds several traversal and searching methods, and with the ability to sort.

## Create Array
Expand All @@ -13,7 +15,8 @@ array = [1, "two", 3.0] #=> [1, "two", 3.0]

## Element Assignment

Elements can be accessed or changed using indexes. Subarrays can be accessed by specifying a start index and a size.
Elements can be accessed or changed using indexes.
Subarrays can be accessed by specifying a start index and a size.

```ruby
a = ["", "", "", "", ""]
Expand All @@ -33,7 +36,8 @@ a #=> ["a", "Z"]

## Element Reference

- Elements in an array can be retrieved using the #[] method. It returns the element at index, or returns a subarray starting at the start index and continuing for length elements.
- Elements in an array can be retrieved using the #[] method.
- It returns the element at index, or returns a subarray starting at the start index and continuing for length elements.

```ruby
a = [ "a", "b", "c", "d", "e" ]
Expand All @@ -54,7 +58,8 @@ a[-3, 3] #=> [ "c", "d", "e" ]

## Obtaining Information about an Array

Arrays keep track of their own length at all times. To query an array about the number of elements it contains, use length, count or size.
Arrays keep track of their own length at all times.
To query an array about the number of elements it contains, use length, count or size.

```ruby
browsers = ['Chrome', 'Firefox', 'Safari', 'Opera', 'IE']
Expand All @@ -65,7 +70,7 @@ browsers.size #=> 5

## Adding Items to Arrays

Items can be added to the end of an array by using either push or <<
Items can be added to the end of an array by using either `push` or `<<`:

```ruby
arr = [1, 2, 3, 4]
Expand All @@ -75,10 +80,10 @@ arr << 6 #=> [1, 2, 3, 4, 5, 6]

## Removing Items from an Array

The method pop removes the last element in an array and returns it
The method pop removes the last element in an array and returns it:

```ruby
arr = [1, 2, 3, 4, 5, 6]
arr = [1, 2, 3, 4, 5, 6]
arr.pop #=> 6
arr #=> [1, 2, 3, 4, 5]
```
Expand Down
13 changes: 9 additions & 4 deletions concepts/basics/about.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# About

Ruby is a dynamic and strongly typed language. In dynamic languages the type of a variable or object is resolved at runtime, which means that its value or type can be changed up to the very last moment (when it gets parsed by the interpreter).
And what do we mean with strongly typed? Once we know the type of a variable or object, Ruby is strict about what you can do with it, for example:
Ruby is a dynamic and strongly typed language.
In dynamic languages the type of a variable or object is resolved at runtime, which means that its value or type can be changed up to the very last moment (when it gets parsed by the interpreter).
And what do we mean with strongly typed?
Once we know the type of a variable or object, Ruby is strict about what you can do with it, for example:

```ruby
x = '2'
y = x + 'n'
# => '2n'
# => '2n'
```

**But**
Expand All @@ -17,7 +19,10 @@ y = x + 2
# => TypeError (no implicit conversion of Integer into String)
```

Remember, in Ruby everything is an object. Even classes are instances of the class `Class`. For example:
Remember, in Ruby everything is an object.
Even classes are instances of the class `Class`.

For example:

```ruby
1.class
Expand Down
24 changes: 19 additions & 5 deletions concepts/basics/introduction.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# Introduction

Ruby is a dynamic [object-oriented language][object-oriented-programming]. Everything in Ruby is an [object][object].
Ruby is a dynamic [object-oriented language][object-oriented-programming].
Everything in Ruby is an [object][object].

There are two primary ways to assign objects to names in Ruby - using variables or constants. Variables are always written in [snake case][snake-case]. A variable can reference different objects over its lifetime. For example, `my_first_variable` can be defined and redefined many times using the `=` operator:
There are two primary ways to assign objects to names in Ruby - using variables or constants.
Variables are always written in [snake case][snake-case].
A variable can reference different objects over its lifetime.
For example, `my_first_variable` can be defined and redefined many times using the `=` operator:

```ruby
my_first_variable = 1
my_first_variable = "Some string"
my_first_variable = SomeComplexObject.new
```

Constants, however, are meant to be assigned once. They must start with capital letters and are normally written in block capitals with words separated by underscores. For example:
Constants, however, are meant to be assigned once.
They must start with capital letters and are normally written in block capitals with words separated by underscores.

For example:

```ruby
MY_FIRST_CONSTANT = 10
Expand All @@ -19,7 +26,11 @@ MY_FIRST_CONSTANT = 10
# MY_FIRST_CONSTANT = "Some String"
```

Ruby is organised into classes. Classes are defined using the `class` keyword followed by the name of the class. Objects are generally created by instantiating classes using the `.new` method. For example:
Ruby is organised into classes.
Classes are defined using the `class` keyword followed by the name of the class.
Objects are generally created by instantiating classes using the `.new` method.

For example:

```ruby
# Define the class
Expand All @@ -31,7 +42,10 @@ end
my_first_calc = Calculator.new
```

Units of functionality are encapsulated in methods - similar to _functions_ in other languages. A method can optionally be defined with positional arguments, and/or keyword arguments that are defined and called using the `:` syntax. Methods either implicitly return the result of the last evaluated statement, or can explicitly return an object via the `return` keyword. Methods are invoked using `.` syntax.
Units of functionality are encapsulated in methods - similar to _functions_ in other languages.
A method can optionally be defined with positional arguments, and/or keyword arguments that are defined and called using the `:` syntax.
Methods either implicitly return the result of the last evaluated statement, or can explicitly return an object via the `return` keyword.
Methods are invoked using `.` syntax.

```ruby
class Calculator
Expand Down
6 changes: 3 additions & 3 deletions concepts/blocks/about.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# About

Blocks are small groupings of statements that can be executed multiple times.
They can be thought of as closures or anonymous functions.
Blocks are defined using the `do...end` syntax (above), or the `{}` (below).
Blocks are small groupings of statements that can be executed multiple times.
They can be thought of as closures or anonymous functions.
Blocks are defined using the `do...end` syntax (above), or the `{}` (below).
The styles are interchangeable and differing opinions exist about when each should be used.

## Shortcut Syntax
Expand Down
6 changes: 3 additions & 3 deletions concepts/blocks/introduction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Introduction

Blocks are small groupings of statements that can be executed multiple times.
They can be thought of as closures or anonymous functions.
Blocks are defined using the `do...end` syntax (above), or the `{}` (below).
Blocks are small groupings of statements that can be executed multiple times.
They can be thought of as closures or anonymous functions.
Blocks are defined using the `do...end` syntax (above), or the `{}` (below).
The styles are interchangeable and differing opinions exist about when each should be used.
9 changes: 6 additions & 3 deletions concepts/booleans/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## True and False

True and false logical states are represented with `true` and `false` in Ruby. These may either be used as literals on their own, or as a result of logical or comparison methods.
True and false logical states are represented with `true` and `false` in Ruby.
These may either be used as literals on their own, or as a result of logical or comparison methods.

```ruby
happy = true
Expand All @@ -21,7 +22,8 @@ When evaluating objects in `if` statements or other boolean contexts, all object

## Control flow

_Truthy_ and _falsey_ evaluations are useful in the context of control flow. Like in procedural languages, Ruby has an `if`...`else` construct, but it may be more common to use `if` as a "guarding" statement to modify the evaluation of an expression.
_Truthy_ and _falsey_ evaluations are useful in the context of control flow.
Like in procedural languages, Ruby has an `if`...`else` construct, but it may be more common to use `if` as a "guarding" statement to modify the evaluation of an expression.

```ruby
def falsey
Expand Down Expand Up @@ -49,7 +51,8 @@ end
# the numbers are not added because of the modifier, nil is returned
```

Ruby provides `unless` to make code read well. E.g.) Rather than `eat_desert if not too_full`, we can also write `eat_desert unless too_full`.
Ruby provides `unless` to make code read well.
E.g.) Rather than `eat_desert if not too_full`, we can also write `eat_desert unless too_full`.

```ruby
3 + 3 unless truthy
Expand Down
9 changes: 7 additions & 2 deletions concepts/exceptions/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ We do this using the `raise` method, passing in an object - normally an Exceptio
For example, you'll see in the exercise stubs that we use the built-in `RuntimeError` to tell Ruby that a method hasn't been implemented.
You can also use the shorthand syntax of `raise(ExceptionObject, params)`.
If the exception class is omitted, `RuntimeError` is used by default.

For example:

```ruby
Expand All @@ -18,7 +19,9 @@ raise "Please implement this method"
```

When Ruby sees this it bubbles the error to the top of the program and then exits.

For example, if you try dividing something by zero, you will see something like this:

```ruby
5/0

Expand All @@ -32,7 +35,8 @@ Exceptions should not be used for control-flow of a program, as that is consider

## Class hierarchy

In Ruby exceptions follow a class hierarchy where `Exception` is the base class. These are the most common Ruby's built-in exceptions:
In Ruby exceptions follow a class hierarchy where `Exception` is the base class.
These are the most common Ruby's built-in exceptions:

```
Exception
Expand Down Expand Up @@ -64,7 +68,8 @@ Exception
SystemExit
```

Rescuing errors of a specific class also rescues errors of its children. This is why rescuing from `Exception` can be dangerous.
Rescuing errors of a specific class also rescues errors of its children.
This is why rescuing from `Exception` can be dangerous.
Ruby uses exceptions to also handle messages from the operative system "Signals", for example `ctrl-c`.
This means that rescuing from `Exception` will also capture this system "Signals".
So in order to prevent unexpected behaviours the common practice to capture "all errors" is to rescue form `StandardError`.
Expand Down
2 changes: 1 addition & 1 deletion concepts/exceptions/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Exceptions are a form of error handling.
They are called exceptions, as they normally appear when dealing with some unexpected event.

At any point in our code, we can "raise" an exception.
At any point in our code, we can "raise" an exception.
We do this using the `raise` method, passing in an object - normally an Exception object, although we can also use basic strings.
For example, you'll see in the exercise stubs that we use the built-in `RuntimeError` to tell Ruby that a method hasn't been implemented.
You can also use the shorthand syntax of `raise(ExceptionObject, params)`.
Expand Down
16 changes: 13 additions & 3 deletions concepts/floating-point-numbers/about.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
# About

A floating-point number is a number with zero or more digits behind the decimal separator. Examples are `4.0`, `0.1`, `3.14`, `-6.4` `16.984025` and `1024.0`. In Ruby, floating-point numbers are implemented through the [Float][Float] class.
A floating-point number is a number with zero or more digits behind the decimal separator.
Examples are `4.0`, `0.1`, `3.14`, `-6.4` `16.984025` and `1024.0`.
In Ruby, floating-point numbers are implemented through the [Float][Float] class.

You can find a short introduction to floating-point numbers at [0.30000000000000004.com][0.30000000000000004.com].

The [Float Toy page][evanw.github.io-float-toy] has a nice, graphical explanation how a floating-point numbers' bits are converted to an actual floating-point value.

To repeatedly execute logic, one can use loops. In this example the `while` loop is useful because it keeps on looping _while_ a condition evaluates to some truthy value (i.e. not `false` or `nil`). Ruby implements a loop similar to the `while` loop. It's called the `until` loop, and you've probably guessed what it does. It keeps looping _until_ a boolean condition evaluates to `true`. In some languages, to make a piece of code execute an unlimited number of times, constructs like `while true` are used. In Ruby, the `loop` loop exists for that purpose. Even though the `loop` loop does not depend on a single condition, it can be canceled by using a `return` or `break` keyword.
To repeatedly execute logic, one can use loops.
In this example the `while` loop is useful because it keeps on looping _while_ a condition evaluates to some truthy value (i.e. not `false` or `nil`).
Ruby implements a loop similar to the `while` loop.
It's called the `until` loop, and you've probably guessed what it does.
It keeps looping _until_ a boolean condition evaluates to `true`.
In some languages, to make a piece of code execute an unlimited number of times, constructs like `while true` are used.
In Ruby, the `loop` loop exists for that purpose.
Even though the `loop` loop does not depend on a single condition, it can be canceled by using a `return` or `break` keyword.

The `#years_before_desired_balance` method from the previous exercise could have been written by using any of the three mentioned loops:

Expand Down Expand Up @@ -49,7 +58,8 @@ def self.years_before_desired_balance(current_balance, desired_balance)
end
```

As you have probably noticed, Ruby has no increment operator (`i++`) like some other languages do. Instead, constructs like `i += 1` (which is equal to `i = i + 1`) can be used.
As you have probably noticed, Ruby has no increment operator (`i++`) like some other languages do.
Instead, constructs like `i += 1` (which is equal to `i = i + 1`) can be used.

[Float]: https://docs.ruby-lang.org/en/master/Float.html
[0.30000000000000004.com]: https://0.30000000000000004.com/
Expand Down
3 changes: 2 additions & 1 deletion concepts/floating-point-numbers/introduction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Loops

A floating-point number is a number with zero or more digits behind the decimal separator. Examples are `4.0`, `0.1`, `3.14`, `-6.4` `16.984025` and `1024.0`.
A floating-point number is a number with zero or more digits behind the decimal separator.
Examples are `4.0`, `0.1`, `3.14`, `-6.4` `16.984025` and `1024.0`.
In Ruby, floating-point numbers are implemented through the [Float][Float] class.

[Float]: https://docs.ruby-lang.org/en/master/Float.html
3 changes: 2 additions & 1 deletion concepts/instance-variables/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class Backpack
end
```

- Methods named with a trailing `=` are recognized as setters by Ruby, and allow the syntactic "sugar" use of the assignment syntax, e.g. `Backpack.new("Sven").owner = "Ayah"`. Notice the space between `owner` and `=` while the actual method name is `owner=`.
- Methods named with a trailing `=` are recognized as setters by Ruby, and allow the syntactic "sugar" use of the assignment syntax, e.g. `Backpack.new("Sven").owner = "Ayah"`.
Notice the space between `owner` and `=` while the actual method name is `owner=`.
- Getters and setters can be created using the `attr_reader`, `attr_writer`, and `attr_accessor` methods:
- `attr_reader`: Create getters for the symbols listed
- `attr_writer`: Create setters for the symbols listed
Expand Down
3 changes: 2 additions & 1 deletion concepts/loops/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ def self.years_before_desired_balance(current_balance, desired_balance)
end
```

As you have probably noticed, Ruby has no increment operator (`i++`) like some other languages do. Instead, constructs like `i += 1` (which is equal to `i = i + 1`) can be used.
As you have probably noticed, Ruby has no increment operator (`i++`) like some other languages do.
Instead, constructs like `i += 1` (which is equal to `i = i + 1`) can be used.
3 changes: 2 additions & 1 deletion concepts/modules/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Some times you don't need the overhead of creating an object with state.
In these cases, a `module` can be used.

A module is very similar to a class (in fact, `Module` is `Classes` parent in the object hierarchy) - the main difference being that rather than using instance methods, we use class methods.
Class methods start with `self.` and are directly called on a module.
Class methods start with `self.` and are directly called on a module.

For example:

```ruby
Expand Down
3 changes: 2 additions & 1 deletion concepts/modules/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Some times you don't need the overhead of creating an object with state.
In these cases, a `module` can be used.

A module is very similar to a class (in fact, `Module` is an ancestor of `Class` in the object hierarchy) - the main difference being that rather than using instance methods, we use class methods.
Class methods start with `self.` and are directly called on a module.
Class methods start with `self.` and are directly called on a module.

For example:

```ruby
Expand Down
3 changes: 1 addition & 2 deletions concepts/multiple-assignment-and-decomposition/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,7 @@ This will pack all **key**/**value** pairs from one hash into another hash, or c
### Composition with method parameters

When you create a method that accepts an arbitrary number of arguments, you can use [`*arguments`][arguments] or [`**keyword_arguments`][keyword arguments] in the method definition.
`*arguments` is used to pack an arbitrary number of positional (non-keyworded) arguments and
`**keyword_arguments` is used to pack an arbitrary number of keyword arguments.
`*arguments` is used to pack an arbitrary number of positional (non-keyworded) arguments and `**keyword_arguments` is used to pack an arbitrary number of keyword arguments.

Usage of `*arguments`:

Expand Down
4 changes: 3 additions & 1 deletion concepts/nil/about.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# About

[Nil][nil-dictionary] is an English word meaning "nothing" or "zero". In Ruby, `nil` is an object which is used to express the _absence_ of a value. In other programming languages, `null` or `none` values may play a similar role.
[Nil][nil-dictionary] is an English word meaning "nothing" or "zero".
In Ruby, `nil` is an object which is used to express the _absence_ of a value.
In other programming languages, `null` or `none` values may play a similar role.

```ruby
# I do not have a favorite color
Expand Down
5 changes: 3 additions & 2 deletions concepts/nil/introduction.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Introduction


[Nil][nil-dictionary] is an English word meaning "nothing" or "zero". In Ruby, `nil` is used to express the _absence_ of an object. In other programming languages, `null` or `none` values may play a similar role.
[Nil][nil-dictionary] is an English word meaning "nothing" or "zero".
In Ruby, `nil` is used to express the _absence_ of an object.
In other programming languages, `null` or `none` values may play a similar role.

```ruby
# I do not have a favorite color
Expand Down
Loading
Loading