Skip to content

Instantly share code, notes, and snippets.

@codefrau
Last active June 12, 2024 03:54
Show Gist options
  • Save codefrau/3dc9e1a3020bb92864c57c4323041aec to your computer and use it in GitHub Desktop.
Save codefrau/3dc9e1a3020bb92864c57c4323041aec to your computer and use it in GitHub Desktop.
DNU to get/set inst vars
doesNotUnderstand: aMessage
"gets or sets an instance variable"
| argCount getters key setters value index |
argCount := aMessage numArgs.
argCount > 1 ifTrue: [ ^super doesNotUnderstand: aMessage ].
key := aMessage keywords first.
getters := self class allInstVarNames.
index := getters indexOf: key.
index > 0 ifTrue: [^self instVarAt: index].
setters := getters collect: [ :name | name, ':' ].
index := setters indexOf: key.
index > 0 ifTrue: [
value := aMessage arguments first.
^self instVarAt: index put: value].
^super doesNotUnderstand: aMessage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment