Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby: Provide descriptive link text #28322

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions ruby/basic_ruby/basic_enumerable_methods.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Introduction

Check failure on line 1 in ruby/basic_ruby/basic_enumerable_methods.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Headings should be surrounded by blank lines

ruby/basic_ruby/basic_enumerable_methods.md:1 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "### Introduction"] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md022.md
In previous lessons, you learned about loops as well as arrays and hashes. You will soon discover that you'll have to do so much iterating (looping or repeating something several times) over collections as a developer that it will make you dizzy. Remember the **DRY (Don't Repeat Yourself)** approach to programming that we talked about in the lesson on Methods? Well, Ruby keeps iterating DRY through something called enumerables.

**Enumerables** are a set of convenient built-in methods in Ruby that are included as part of both arrays and hashes. There are some iteration patterns that you'll find yourself doing again and again, such as transforming, searching for, and selecting subsets of elements in your collections. Enumerables were designed to make implementing these iteration patterns (and therefore your life as a developer) much, much easier.
Expand All @@ -18,7 +18,7 @@
- Describe the differences between using `do...end` and `{...}`.
- Explain what a bang method is and why it is or is not considered best practice.

### Life before enumerables

Check failure on line 21 in ruby/basic_ruby/basic_enumerable_methods.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Headings should be surrounded by blank lines

ruby/basic_ruby/basic_enumerable_methods.md:21 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "### Life before enumerables"] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md022.md
Let's say that you wanted to make an invite list for your birthday using your `friends` array but that you don't want to invite your friend Brian because he's a bit of a nutcase at parties and always drinks way too much.

With the loops you've learned so far, you might do something like this:
Expand All @@ -34,8 +34,8 @@
end

invited_list #=> ["Sharon", "Leo", "Leila", "Arun"]
```

Check failure on line 37 in ruby/basic_ruby/basic_enumerable_methods.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Fenced code blocks should be surrounded by blank lines

ruby/basic_ruby/basic_enumerable_methods.md:37 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```"] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md031.md
_Warning:_ The __do__ is optional in a for loop in Ruby and may cause issues if used in IRB

Check failure on line 38 in ruby/basic_ruby/basic_enumerable_methods.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Emphasis style

ruby/basic_ruby/basic_enumerable_methods.md:38:1 MD049/emphasis-style Emphasis style [Expected: asterisk; Actual: underscore] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md049.md

Check failure on line 38 in ruby/basic_ruby/basic_enumerable_methods.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Emphasis style

ruby/basic_ruby/basic_enumerable_methods.md:38:10 MD049/emphasis-style Emphasis style [Expected: asterisk; Actual: underscore] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md049.md

Check failure on line 38 in ruby/basic_ruby/basic_enumerable_methods.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Strong style

ruby/basic_ruby/basic_enumerable_methods.md:38:16 MD050/strong-style Strong style [Expected: asterisk; Actual: underscore] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md050.md

Check failure on line 38 in ruby/basic_ruby/basic_enumerable_methods.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Strong style

ruby/basic_ruby/basic_enumerable_methods.md:38:20 MD050/strong-style Strong style [Expected: asterisk; Actual: underscore] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md050.md

That's not too hard, but imagine having to do that for every party you host from now until the end of time! It might be easier to just stop hanging out with Brian.

Expand All @@ -59,7 +59,7 @@

You just cut down what was previously an 8 line program down to 2 lines. Amazing! Imagine all the time you'll save sorting your invite lists now.

### The each method

Check failure on line 62 in ruby/basic_ruby/basic_enumerable_methods.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Headings should be surrounded by blank lines

ruby/basic_ruby/basic_enumerable_methods.md:62 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "### The each method"] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md022.md
`#each` is the granddaddy of the enumerable methods. It's a bit like Chuck Norris: it can do anything. As you'll see throughout this lesson, though, just because you can use `#each` to do just about anything doesn't mean it's always the best or most efficient tool for the job.

Calling `#each` on an array will iterate through that array and will yield each element to a code block, where a task can be performed:
Expand All @@ -80,8 +80,8 @@

Let's break down this syntax:

* `friends` is the array that contains strings of your friends' names.

Check failure on line 83 in ruby/basic_ruby/basic_enumerable_methods.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Unordered list style

ruby/basic_ruby/basic_enumerable_methods.md:83:1 MD004/ul-style Unordered list style [Expected: dash; Actual: asterisk] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md004.md
* `.each` is the enumerable method you are calling on your `friends` array.

Check failure on line 84 in ruby/basic_ruby/basic_enumerable_methods.md

View workflow job for this annotation

GitHub Actions / Lint lesson files

Unordered list style

ruby/basic_ruby/basic_enumerable_methods.md:84:1 MD004/ul-style Unordered list style [Expected: dash; Actual: asterisk] https://github.com/DavidAnson/markdownlint/blob/v0.32.1/doc/md004.md
* `{ |friend| puts "Hello, " + friend }` is a **block**, and the code inside this block is run for each element in your array. Because we have 5 friends in our array, this block will be run 5 times, once with each of the 5 elements.
* Within the block, you'll notice that we have `|friend|`, which is known as a **block variable**. This is the element from your array that the block is currently iterating over. You can use any variable name that you find helpful here; in this example, we could have used `|x|`, but `|friend|` is more descriptive of what each element is. In the first iteration, the value of `|friend|` will be `'Sharon'`; in the second iteration, its value will be `'Leo'`; in the third, `'Leila'`; and so on until it reaches the end of the array.

Expand Down Expand Up @@ -421,7 +421,7 @@
1. Read through the Ruby Explained article on [Map, Select, and Other Enumerable Methods](https://www.eriktrautman.com/posts/ruby-explained-map-select-and-other-enumerable-methods).
2. Follow along with this [How to Use The Ruby Map Method](https://www.rubyguides.com/2018/10/ruby-map-method/) tutorial from Ruby Guides.
3. Follow along with this [Reducing Enumerable](https://medium.com/@baweaver/reducing-enumerable-part-one-the-journey-begins-ddc1d4108490) article by Brandon Weaver.
4. Complete the [basic enumerable](https://github.com/TheOdinProject/ruby-exercises/tree/main/ruby_basics) exercises from the [ruby-exercises repo](https://github.com/TheOdinProject/ruby-exercises) that you previously cloned.
4. Complete the basic enumerable exercises from the [ruby-exercises repo](https://github.com/TheOdinProject/ruby-exercises) that you previously cloned.
</div>

### Knowledge check
Expand All @@ -440,5 +440,5 @@
This section contains helpful links to related content. It isn't required, so consider it supplemental.

- The Bastards Book of Ruby has a good section on [Enumerables](http://ruby.bastardsbook.com/chapters/enumerables/).
- This tutorial on [codementor](https://www.codementor.io/ruby-on-rails/tutorial/rubys-swiss-army-knife-the-enumerable-module) is another good discussion of the versatility of enumerable methods.
- This tutorial on codementor is another good discussion of [the versatility of enumerable methods](https://www.codementor.io/ruby-on-rails/tutorial/rubys-swiss-army-knife-the-enumerable-module).
- There are many more enumerable methods than are covered in this lesson (e.g., `#reject`, `#drop`, `#uniq`). For a full listing, you can check out the [Ruby Docs](https://docs.ruby-lang.org/en/3.3/Enumerable.html).
10 changes: 5 additions & 5 deletions ruby/basic_ruby/debugging.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Introduction

Tracking down and fixing both errors and unexpected behavior in your code is an inevitable part of being a developer. The art of finding the cause of problems and then fixing them in code is known as **debugging**. The [origin of the term](https://en.wikipedia.org/wiki/Debugging#Etymology) is a classic computer science tale worth reading if you haven't already.
Tracking down and fixing both errors and unexpected behavior in your code is an inevitable part of being a developer. The art of finding the cause of problems and then fixing them in code is known as **debugging**. The [origin of the term "debugging"](https://en.wikipedia.org/wiki/Debugging#Etymology) is a classic computer science tale worth reading if you havent already.

In this lesson, we'll cover all of the main techniques you can use to debug your code when you run into a problem.

Expand All @@ -25,7 +25,7 @@ The stack trace prints each line of code in your program that was executed befor

![First line of stack trace](https://cdn.statically.io/gh/TheOdinProject/curriculum/a2cfa47e944fa8127ccf5faa6e1c7c328de42428/ruby/basic_ruby/debugging/imgs/01.png)

<span id='stack-trace-first-line-info'>First, this line of the stack trace will tell you what specific line caused the runtime error. In the above example, the error was encountered in line 31 of the file `bottles.rb`. This line also provides a brief explanation of the error and the name of the error. (In this case, it's a [`NameError`](https://docs.ruby-lang.org/en/3.3/NameError.html)). And yes, in Ruby, [errors](https://docs.ruby-lang.org/en/3.3/Exception.html) are *also* objects.</span>
<span id='stack-trace-first-line-info'>First, this line of the stack trace will tell you what specific line caused the runtime error. In the above example, the error was encountered in line 31 of the file `bottles.rb`. This line also provides a brief explanation of the error and the name of the error. (In this case, it's a [`NameError`](https://docs.ruby-lang.org/en/3.3/NameError.html)). And yes, in Ruby, [errors (Exceptions) are also objects](https://docs.ruby-lang.org/en/3.3/Exception.html).</span>

There you have it. At this point, you know where in your code the exception is being raised, and you know the type of error you're dealing with. You might even know what fixes need to be implemented in your code.

Expand Down Expand Up @@ -94,15 +94,15 @@ irb(main):013:0> isogram?("Odin")
=> false
```

Indeed, we didn't use `#split` correctly, as this particular creates an array with the given string rather than creating an array of characters of the given string. Why? By default, if we didn't provide arguments, [#split](https://docs.ruby-lang.org/en/3.3/String.html#method-i-split) will divide the string using `whitespace` as the delimiter. Try running the above code in a REPL or IRB using `#split('')` instead, and you'll see the difference.
Indeed, we didn't use `#split` correctly, as this particular creates an array with the given string rather than creating an array of characters of the given string. Why? By default, if we didnt provide arguments, [the #split method](https://docs.ruby-lang.org/en/3.3/String.html#method-i-split) will divide the string using `whitespace` as the delimiter. Try running the above code in a REPL or IRB using `#split('')` instead, and you'll see the difference.

Hostage situation resolved! That wasn't so bad, was it?

#### Debugging with puts and nil

Using `puts` is a great way to debug, but there's a **HUGE** caveat with using it: calling `puts` on anything that is `nil` or an empty string or collection will just print a blank line to your terminal.

This is one instance where using `p` will yield more information. As mentioned above, `p` is a combination of `puts` and [#inspect](https://docs.ruby-lang.org/en/3.3/Object.html#method-i-inspect), the latter of which essentially prints a string representation of whatever it's called on. To illustrate this, try the following in a REPL:
This is one instance where using `p` will yield more information. As mentioned above, `p` is a combination of puts and [the #inspect method](https://docs.ruby-lang.org/en/3.3/Object.html#method-i-inspect), the latter of which essentially prints a string representation of whatever it's called on. To illustrate this, try the following in a REPL:

```ruby
puts "Using puts:"
Expand Down Expand Up @@ -185,7 +185,7 @@ Using the same example above, you can use one of pry-byebug's commands to figure

```

It stops after evaluating the next line. `name` now returns `BOB`. Calling `next` again will evaluate the following line. Try it out to know what `greeting` will return. Pry-byebug has a few more commands, play around with them to get a feel of what they do. You can find the commands with a short description of what they do [in the pry-byebug README](https://github.com/deivid-rodriguez/pry-byebug).
It stops after evaluating the next line. `name` now returns `BOB`. Calling `next` again will evaluate the following line. Try it out to know what `greeting` will return. [Pry-byebug has a few more commands](https://github.com/deivid-rodriguez/pry-byebug). Play around with them to get a feel of what they do.

As you can see, using Pry-byebug for debugging achieves the same outcome as `puts` debugging: it allows you to confirm the assumptions you have about particular parts of your code. If your code is complex, Pry-byebug will probably allow you to debug quicker thanks to its interactive runtime environment. In such scenarios, Pry-byebug will be easier to interact with than having to add `puts` statements everywhere and re-running your code each time.

Expand Down
4 changes: 2 additions & 2 deletions ruby/basic_ruby/predicate_enumerable_methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fruits.none? { |fruit| fruit.length > 6 }
### Assignment
<div class="lesson-content__panel" markdown="1">
1. Read [How to Use Ruby Any, All, None, and One](https://www.rubyguides.com/2018/10/any-all-none-one/) for alternative explanations for predicate enumerables.
2. Complete the [predicate enumerable](https://github.com/TheOdinProject/ruby-exercises/tree/main/ruby_basics) exercises from the [ruby-exercises repo](https://github.com/TheOdinProject/ruby-exercises) that you previously cloned.
2. Complete the predicate enumerable exercises from the [ruby-exercises repo](https://github.com/TheOdinProject/ruby-exercises) that you previously cloned.
</div>

### Knowledge check
Expand All @@ -237,4 +237,4 @@ This section contains questions for you to check your understanding of this less

This section contains helpful links to related content. It isn't required, so consider it supplemental.

- There are many more enumerable methods than are covered in this lesson (e.g., `#member?`). For a full listing, you can check out the [Ruby Docs](https://docs.ruby-lang.org/en/3.3/Enumerable.html).
- It looks like this lesson doesn't have any additional resources yet. Help us expand this section by contributing to our curriculum.
Loading