Use the run_irb
feature to execute markdown-embedded Ruby snippets in the Ruby interactive shell, irb
.
This allows you to interleave Ruby snippets with any markdown (usually explanatory text, but actually anything).
In the example template below, snippets of Ruby are interleaved with other markdown elements.
Each Ruby snippet that's to be executed in irb
is bracketed by pragmas ```#run_irb
and ```
.
In the example, each snippet has some Ruby code that shows values using method p
.
# About Hashes Create a hash: ```#run_irb h = {} p h.class p h ``` Initialize a hash: ```#run_irb h = {:a => 0, :b => 1} p h ``` Change a value: ```#run_irb h[:b] = 2 p h ``` Add a new entry: ```#run_irb h[:c] = 2 p h ``` Delete an entry: ```#run_irb h.delete(:a) p h ```
CLI
markdown_helper run_irb --pristine template.md markdown.md
(Option --pristine
suppresses comment insertion.)
API
run_irb.rb
:
require 'markdown_helper'
# Option :pristine suppresses comment insertion.
markdown_helper = MarkdownHelper.new(:pristine => true)
markdown_helper.run_irb('template.md', 'markdown.md')
The resulting markdown (raw) shows the output from irb
for each snippet:
# About Hashes Create a hash: ```ruby h = {} p h.class Hash p h {} ``` Initialize a hash: ```ruby h = {:a => 0, :b => 1} p h {:a=>0, :b=>1} ``` Change a value: ```ruby h[:b] = 2 p h {:a=>0, :b=>2} ``` Add a new entry: ```ruby h[:c] = 2 p h {:a=>0, :b=>2, :c=>2} ``` Delete an entry: ```ruby h.delete(:a) p h {:b=>2, :c=>2} ```
Resulting markdown (rendered):
Create a hash:
h = {}
p h.class
Hash
p h
{}
Initialize a hash:
h = {:a => 0, :b => 1}
p h
{:a=>0, :b=>1}
Change a value:
h[:b] = 2
p h
{:a=>0, :b=>2}
Add a new entry:
h[:c] = 2
p h
{:a=>0, :b=>2, :c=>2}
Delete an entry:
h.delete(:a)
p h
{:b=>2, :c=>2}