Sha256: 0608c6c5589d0848c62f4b31ee8caee5769626f70d4a0d8bc1168021ed7ddf40

Contents?: true

Size: 1004 Bytes

Versions: 10

Compression:

Stored size: 1004 Bytes

Contents

#!/usr/bin/env ruby

require "Getopt/Declare"


@interpolator = Getopt::Declare.new(<<'EOCMDS',['-BUILD'])
	[cluster:none]
	[repeatable]
	[pvtype: NOTDELIM /(?:%T.)+/ ]
	[pvtype: WS   /\s+/ ]

        \{{ <cmd:NOTDELIM> }}[<ws:WS>]	
			{
  print "cmd=",cmd,"\n"
                          self['result'] += (eval cmd||'').to_s
			  self['result'] += ws if ws
                        }

	<str>[<ws:WS>]	
			{ self['result'] = '' unless self['result']
			  self['result'] += str
			  self['result'] += ws if ws }
EOCMDS

def interpolate(t)
  @interpolator['result'] = ''
  @interpolator.parse(t)
  return @interpolator['result']
end


$result = 22
$name = "Sam"
$n = 50

def average(t)
  sum, count = [0,0]
  t.each { |i| sum += i; count += 1; }
  return count ? sum/count : 0;
end

print interpolate('The person {{$name}} scored {{$result}}'), "\n";
print interpolate('The pass mark was {{$result * 2}}'), "\n";
print interpolate('The average of the first {{2*$n}} numbers is {{average(1..2*$n)}}'), "\n";

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
getopt-declare-1.32 samples/demo_interp.rb
getopt-declare-1.31 samples/demo_interp.rb
getopt-declare-1.30 samples/demo_interp.rb
getopt-declare-1.29 samples/demo_interp.rb
getopt-declare-1.23 samples/demo_interp.rb
getopt-declare-1.25 samples/demo_interp.rb
getopt-declare-1.22 samples/demo_interp.rb
getopt-declare-1.27 samples/demo_interp.rb
getopt-declare-1.26 samples/demo_interp.rb
getopt-declare-1.28 samples/demo_interp.rb