Skip to content

Instantly share code, notes, and snippets.

@elektret
Created September 4, 2013 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elektret/6432728 to your computer and use it in GitHub Desktop.
Save elektret/6432728 to your computer and use it in GitHub Desktop.
Bash script in Ruby DATA section
#!/usr/bin/env ruby -w
function, list, f = {}, [], nil
def function.method_missing(meth, *args, &block)
return self[meth.to_s] if has_key? meth.to_s
raise NoMethodError
end
class Function
@@global = String.new
def self.header(line)
@@global << line.lstrip
end
def initialize(name)
@local = String.new
end
def to_s
@@global + @local
end
def exec
%x| #{to_s} |
end
def <<(line)
@local << line.lstrip
end
end
DATA.each do |line|
case line
when /\Afunction (\w+)/ then function[$1] = f = Function.new $1
else if f.is_a?(Function) then f << (line =~ /\A}/ ? '' : line)
else Function.header line
end
end
end
puts function.hello.exec
puts function.bye.exec
__END__
#!/bin/bash
GREETING='Hello World'
function hello() {
echo "${GREETING}"
}
function bye() {
echo 'Bye Bye'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment