Skip to content

Instantly share code, notes, and snippets.

@jmatsu
Created July 21, 2015 16:51
Show Gist options
  • Save jmatsu/83f438d0f1c6c9cbd93d to your computer and use it in GitHub Desktop.
Save jmatsu/83f438d0f1c6c9cbd93d to your computer and use it in GitHub Desktop.
Sample
function main()
{
Object::class.new Test object1
object1.test_echo
}
function Object::class.new() {
local class=$1
local self=$2
shift 2
if type -t ${self}.initialize > /dev/null; then
"${self}.initialize" "$@"
fi
local m
for m in $(find_instance_methods "${class}::instance.*"); do
# delegate
m=${m#${class}::instance.}
eval "function ${self}.${m}() {
${class}::instance.${m} \"$self\" \"\$@\"
}"
done
}
function Test::instance.test_echo() {
echo "test"
}
function find_instance_methods() {
local pattern=$1
local function_ line
typeset -F | while read line; do
function_=${line##* }
[[ "$function_" == ${pattern} ]] && echo $function_
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment