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: concepts/arrays/about.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,10 @@
1
1
# About
2
2
3
-
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:
3
+
Data structures that can hold zero or more elements are known as _collections_.
4
+
An **array** in Ruby is a collection that maintains the ordering in which its objects are added.
5
+
Arrays can hold any object.
6
+
Objects can be added to an array or retrieved from it using an index.
7
+
Ruby array indexing is zero-based, meaning that the first element's index is always zero:
Copy file name to clipboardExpand all lines: concepts/arrays/introduction.md
+12-7Lines changed: 12 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# Introduction
2
2
3
-
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.
3
+
In Ruby, **arrays** are ordered, integer-indexed collections of any object.
4
+
Array indexing starts at `0`.
5
+
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.
4
6
Ruby arrays mix in the [Enumerable module][enumerable-module], which adds several traversal and searching methods, and with the ability to sort.
Elements can be accessed or changed using indexes. Subarrays can be accessed by specifying a start index and a size.
18
+
Elements can be accessed or changed using indexes.
19
+
Subarrays can be accessed by specifying a start index and a size.
17
20
18
21
```ruby
19
22
a = ["", "", "", "", ""]
@@ -33,7 +36,8 @@ a #=> ["a", "Z"]
33
36
34
37
## Element Reference
35
38
36
-
- 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.
39
+
- Elements in an array can be retrieved using the #[] method.
40
+
- It returns the element at index, or returns a subarray starting at the start index and continuing for length elements.
37
41
38
42
```ruby
39
43
a = [ "a", "b", "c", "d", "e" ]
@@ -54,7 +58,8 @@ a[-3, 3] #=> [ "c", "d", "e" ]
54
58
55
59
## Obtaining Information about an Array
56
60
57
-
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.
61
+
Arrays keep track of their own length at all times.
62
+
To query an array about the number of elements it contains, use length, count or size.
Copy file name to clipboardExpand all lines: concepts/basics/about.md
+9-4Lines changed: 9 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,14 @@
1
1
# About
2
2
3
-
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).
4
-
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:
3
+
Ruby is a dynamic and strongly typed language.
4
+
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).
5
+
And what do we mean with strongly typed?
6
+
Once we know the type of a variable or object, Ruby is strict about what you can do with it, for example:
5
7
6
8
```ruby
7
9
x ='2'
8
10
y = x +'n'
9
-
# => '2n'
11
+
# => '2n'
10
12
```
11
13
12
14
**But**
@@ -17,7 +19,10 @@ y = x + 2
17
19
# => TypeError (no implicit conversion of Integer into String)
18
20
```
19
21
20
-
Remember, in Ruby everything is an object. Even classes are instances of the class `Class`. For example:
Copy file name to clipboardExpand all lines: concepts/basics/introduction.md
+19-5Lines changed: 19 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,23 @@
1
1
# Introduction
2
2
3
-
Ruby is a dynamic [object-oriented language][object-oriented-programming]. Everything in Ruby is an [object][object].
3
+
Ruby is a dynamic [object-oriented language][object-oriented-programming].
4
+
Everything in Ruby is an [object][object].
4
5
5
-
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:
6
+
There are two primary ways to assign objects to names in Ruby - using variables or constants.
7
+
Variables are always written in [snake case][snake-case].
8
+
A variable can reference different objects over its lifetime.
9
+
For example, `my_first_variable` can be defined and redefined many times using the `=` operator:
6
10
7
11
```ruby
8
12
my_first_variable =1
9
13
my_first_variable ="Some string"
10
14
my_first_variable =SomeComplexObject.new
11
15
```
12
16
13
-
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:
17
+
Constants, however, are meant to be assigned once.
18
+
They must start with capital letters and are normally written in block capitals with words separated by underscores.
19
+
20
+
For example:
14
21
15
22
```ruby
16
23
MY_FIRST_CONSTANT=10
@@ -19,7 +26,11 @@ MY_FIRST_CONSTANT = 10
19
26
# MY_FIRST_CONSTANT = "Some String"
20
27
```
21
28
22
-
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:
29
+
Ruby is organised into classes.
30
+
Classes are defined using the `class` keyword followed by the name of the class.
31
+
Objects are generally created by instantiating classes using the `.new` method.
32
+
33
+
For example:
23
34
24
35
```ruby
25
36
# Define the class
@@ -31,7 +42,10 @@ end
31
42
my_first_calc =Calculator.new
32
43
```
33
44
34
-
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.
45
+
Units of functionality are encapsulated in methods - similar to _functions_ in other languages.
46
+
A method can optionally be defined with positional arguments, and/or keyword arguments that are defined and called using the `:` syntax.
47
+
Methods either implicitly return the result of the last evaluated statement, or can explicitly return an object via the `return` keyword.
Copy file name to clipboardExpand all lines: concepts/booleans/introduction.md
+6-3Lines changed: 6 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,8 @@
2
2
3
3
## True and False
4
4
5
-
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.
5
+
True and false logical states are represented with `true` and `false` in Ruby.
6
+
These may either be used as literals on their own, or as a result of logical or comparison methods.
6
7
7
8
```ruby
8
9
happy =true
@@ -21,7 +22,8 @@ When evaluating objects in `if` statements or other boolean contexts, all object
21
22
22
23
## Control flow
23
24
24
-
_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.
25
+
_Truthy_ and _falsey_ evaluations are useful in the context of control flow.
26
+
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.
25
27
26
28
```ruby
27
29
deffalsey
@@ -49,7 +51,8 @@ end
49
51
# the numbers are not added because of the modifier, nil is returned
50
52
```
51
53
52
-
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`.
54
+
Ruby provides `unless` to make code read well.
55
+
E.g.) Rather than `eat_desert if not too_full`, we can also write `eat_desert unless too_full`.
Copy file name to clipboardExpand all lines: concepts/floating-point-numbers/about.md
+13-3Lines changed: 13 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,21 @@
1
1
# About
2
2
3
-
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.
3
+
A floating-point number is a number with zero or more digits behind the decimal separator.
4
+
Examples are `4.0`, `0.1`, `3.14`, `-6.4``16.984025` and `1024.0`.
5
+
In Ruby, floating-point numbers are implemented through the [Float][Float] class.
4
6
5
7
You can find a short introduction to floating-point numbers at [0.30000000000000004.com][0.30000000000000004.com].
6
8
7
9
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.
8
10
9
-
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.
11
+
To repeatedly execute logic, one can use loops.
12
+
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`).
13
+
Ruby implements a loop similar to the `while` loop.
14
+
It's called the `until` loop, and you've probably guessed what it does.
15
+
It keeps looping _until_ a boolean condition evaluates to `true`.
16
+
In some languages, to make a piece of code execute an unlimited number of times, constructs like `while true` are used.
17
+
In Ruby, the `loop` loop exists for that purpose.
18
+
Even though the `loop` loop does not depend on a single condition, it can be canceled by using a `return` or `break` keyword.
10
19
11
20
The `#years_before_desired_balance` method from the previous exercise could have been written by using any of the three mentioned loops:
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.
61
+
As you have probably noticed, Ruby has no increment operator (`i++`) like some other languages do.
62
+
Instead, constructs like `i += 1` (which is equal to `i = i + 1`) can be used.
0 commit comments