Markdown Heading Parser from Scratch in Ruby

This guide examines how to parse a Ruby Heredoc object and convert any lines with a hashtag to HTML H1 headings.

Sign Up to Start Your Own Code Camp

large

View Video

Summary

Parse a Ruby Heredoc and replace any lines that start with a hashtag with HTML H1 heading tags.

Exercise File

Code File

Exercise Description

Given a Ruby Heredoc string, replace any lines that start with a hashtag and wrap them in HTML H1 heading tags.

Sample Input

content = <<-EOF
# Hey there
Some content
# Another heading
More content
EOF

Expected Output

"<h1>Hey there</h1>\n\nSome content\n\n<h1>Another heading</h1>\n\nMore content\n"

Real World Usage

When it comes to working with Rails helper methods, it's important to understand how to parse large strings and replace patterns with other strings.

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

Jordan Hudgens

I've been a software engineer for the past decade and have traveled the world building applications and training individuals on a wide variety of topics.


View All Posts