[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-smalltalk] Defining methods within a block
From: |
Rob Hoelz |
Subject: |
[Help-smalltalk] Defining methods within a block |
Date: |
Mon, 12 Nov 2007 03:38:29 -0600 |
Hello Smalltalkers,
I've been trying to immerse myself in Smalltalk for a project I'm
working on, so I've been dabbling in the reflective and introspective
capabilities of the GNU Smalltalk system. I've written a method for
Object that mimics Ruby's "Object-specific class" feature (if you're
not familiar with it, here's a pretty good explanation:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/classes.html#UB), and
here it is, along with some example usage code:
!Object methodsFor: 'my methods'!
objectSpecificMethod: aMethod
| klass className newClassName |
klass := self class.
className := klass name.
newClassName := className , '_1'. "This will have to be changed"
klass subclass: (newClassName asSymbol)
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: (klass category).
(Smalltalk at: (newClassName asSymbol)) compile: aMethod.
self changeClassTo: (Smalltalk at: (newClassName asSymbol)).
^self.
! !
|obj|
obj := Foo new: 1 next: 2 last: 3.
Transcript print: (obj doStuff: 6); cr.
obj objectSpecificMethod: '
doStuff: arg
^(three + arg).
'.
Transcript print: (obj doStuff: 6); cr.
My question is this: Instead of providing Object#objectSpecificMethod
a string, would it be possible to provide it a block? How would one
define a method within a block? I tried
obj objectSpecificMethod: [
doStuff: arg
^(three + args).
].
but that generates a syntax error. Please let me know what you think.
Thanks,
Rob Hoelz
- [Help-smalltalk] Defining methods within a block,
Rob Hoelz <=