June 2017
Beginner
330 pages
7h 30m
English
As great as gsub can be by itself, it can become even more powerful when used in conjunction with blocks. Blocks are incredibly useful tools that we'll be using throughout the rest of this book, so we will go into more detail on exactly what blocks are later on. For now, just think of blocks as being tools that allow you to extend the functionality of a method. Let's take a look at another example of how to use gsub, but this time we're going to pass in a block:
content = "# My Great Headline" content.gsub(/^.*#.*/) { |heading| "<h1>#{heading[2..-1]}</h1>" }
Woah, I know that this code may look weird, don't let it scare you away; let's analyze every process that is going on, along with what the code does. This is the ...
Read now
Unlock full access