-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmethods_ex
executable file
·36 lines (28 loc) · 987 Bytes
/
methods_ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
#
# Introduces `methods` and their association with structs.
readonly DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. ${DIR}/../gobash
function Request() {
local -r url="${1}"
shift 1
make_ $FUNCNAME \
"url" "${url}"
}
# Adding a method (named `curl`) for the `struct` `Request`. Note that
# the method name has to be prefixed by the name of the struct.
function Request_curl() {
# The first argument of each method is an instance on which
# the method was invoked (think of it as `this` or
# `self`). Subsequent arguments (if any) are given to the
# method at invocation time.
local -r req="${1}"
shift 1
# The method body can be anything.
# The exit code from this method will be propagated to the caller.
curl "$($req url)" 2>&1
}
# Create an instance.
req=$(Request "https://www.google.com")
# Invoke a method on the instance.
$req curl