Respond to an unknown method call: Difference between revisions

added slate language
(Undo curly-to-straight quotes in revision 40792 by Spoon! (Talk) — assuming it was accidental since not mentioned in the edit summary)
(added slate language)
Line 175:
example.ding("dong") # prints “tried to handle unknown method ding”
# prints “it had arguments: ["dong"]”</lang>
 
=={{header|Slate}}==
 
Here is an example of unknown methods being used to call shell commands (this is already defined in the base image):
 
<lang slate>
define: #shell &builder: [lobby newSubSpace].
 
_@shell didNotUnderstand: message at: position
"Form a command string and execute it."
[
position > 0
ifTrue: [resend]
ifFalse:
[([| :command |
message selector isUnarySelector ifTrue:
[command ; message selector.
message optionals pairsDo:
[| :key :value |
command ; ' -' ; (key as: String) allButFirst allButLast ; ' ' ; (value as: String)]].
message selector isKeywordSelector ifTrue:
[| keywords args |
keywords: ((message selector as: String) splitWith: $:).
command ; keywords first.
keywords size = 1 ifTrue: "Read a string or array of arguments."
[args: message arguments second.
(args is: String) ifTrue: [command ; ' ' ; args]
ifFalse: [args do: [| :arg | command ; ' ' ; arg]]]]] writingAs: String)
ifNil: [resend] ifNotNilDo: [| :cmd | [Platform run: cmd]]]
].
</lang>
 
Here is an example of it being used:
 
<lang slate>
slate[1]> shell ls: '*.image'.
kernel.new.little.64.1244260494374694.image slate2.image
net.image slate.image
True
slate[2]>
</lang>
 
=={{header|Tcl}}==
Anonymous user