Module: SimpleExample

Extended by:
SimpleExample
Included in:
SimpleExample
Defined in:
lib/simple_example.rb

Defined Under Namespace

Classes: Format

Instance Method Summary collapse

Instance Method Details

#example(&block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/simple_example.rb', line 35

def example(&block)
  result = block.call

  statement = block.to_ruby
  statement.gsub!(/\Aproc \{/,'')
  statement.gsub!(/\}\Z/,'')

  lines = statement.strip.split(/\n/)
  lines.each do |line|
    src_str = Format.source_prefix + line.strip
    res_str = Format.result_prefix + eval(line.strip, block.binding, Pathname(__FILE__).basename, __LINE__).inspect
    sep_str = Format.compact? ? " " : "\n"

    Kernel.puts "#{src_str}#{sep_str}#{res_str}"
  end
  Kernel.puts Format.separator unless Format.separator.empty?

  result
end