Join Kevin Skoglund for an in-depth discussion in this video What is a code block?, part of Ruby Essential Training.
In this chapter we are going to take a closer look at Ruby's code blocks.…We briefly touched on code blocks back when we working with loops and the iterators.…But let's start out by looking at what is a code block and reminding ourselves.…When we had an iterator like 5 times, we had a code block and the code block was…a block of code that we wanted to be executed each time through the loop,…each iteration and that block of code is defined between the do and the end.…So everything between do and end is our code block. In this case it just simply puts Hello.…Now do end is what we use for multiline code blocks.…
That's where it really is the most benefit because we can just keep putting lots…and lots of lines in there.…It could be a hundred lines of code that would be executed five times.…Now if we have something that's so simple like puts Hello, then we saw that we…can also use the shorthand notation which is just the curly braces.…So 5.times puts Hello.…If we have something that is that short this can be much more efficient,…
- Using Ruby in the Interactive Ruby Shell and in standalone scripts
- Learning to write custom code blocks to find, merge, and sort
- Using modules for namespacing or as mix-ins
- Reading from and writing to files
- Creating a full Ruby project from start to finish
Skill Level Beginner
1. Getting Started with Ruby
2. Ruby Object Types
3. Control Structures
4. Code Blocks
8. Working with Files
9. Ruby Project: Creating the Food Finder
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.