This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-09-05 06:59:20

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Smalltalk code for getting a method's content?

Does anyone know of one?

Offline

 

#2 2011-09-05 07:08:02

Baderous
New Scratcher
Registered: 2011-04-14
Posts: 100+

Re: Smalltalk code for getting a method's content?

You'll have to use reflection. Inspect the Behavior class.

Offline

 

#3 2011-09-07 17:32:56

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Smalltalk code for getting a method's content?

Baderous wrote:

You'll have to use reflection. Inspect the Behavior class.

Hmm, I looked, and I didn't see much.  Plus, I just want to bump this so it gets answered.   big_smile

Offline

 

#4 2011-09-07 19:24:38

Baderous
New Scratcher
Registered: 2011-04-14
Posts: 100+

Re: Smalltalk code for getting a method's content?

"accessing method dictionary" is a clear name that points out where you have to search. After looking at that protocol's methods you find 2 of your interest: #sourceCodeAt: and #sourceMethodAt:. Choose the one that fits you better. To test, open an Inspector window on some morph, for intance a sprite, and then type:

Code:

self class sourceCodeAt: #attributeNames

Here I chose the #attributeNames method, but you can choose any other one.

Offline

 

#5 2011-09-07 20:09:26

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Smalltalk code for getting a method's content?

Baderous wrote:

"accessing method dictionary" is a clear name that points out where you have to search. After looking at that protocol's methods you find 2 of your interest: #sourceCodeAt: and #sourceMethodAt:. Choose the one that fits you better. To test, open an Inspector window on some morph, for intance a sprite, and then type:

Code:

self class sourceCodeAt: #attributeNames

Here I chose the #attributeNames method, but you can choose any other one.

Hmmm.  Nice find, Baderous.  Maybe I'm doing something wrong though, but it only seems to work on instance methods.  For example, what you posted works, but on a sprite, the following code returns the error "key not found":

Code:

self class sourceCodeAt: #blockSpecs

Maybe I'm making some silly mistake though.  So you have any idea why it won't work?

Offline

 

#6 2011-09-08 03:15:40

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Smalltalk code for getting a method's content?

Greenatic wrote:

Baderous wrote:

"accessing method dictionary" is a clear name that points out where you have to search. After looking at that protocol's methods you find 2 of your interest: #sourceCodeAt: and #sourceMethodAt:. Choose the one that fits you better. To test, open an Inspector window on some morph, for intance a sprite, and then type:

Code:

self class sourceCodeAt: #attributeNames

Here I chose the #attributeNames method, but you can choose any other one.

Hmmm.  Nice find, Baderous.  Maybe I'm doing something wrong though, but it only seems to work on instance methods.  For example, what you posted works, but on a sprite, the following code returns the error "key not found":

Code:

self class sourceCodeAt: #blockSpecs

Maybe I'm making some silly mistake though.  So you have any idea why it won't work?

Try with a colon at the end.  wink

Offline

 

#7 2011-09-08 10:05:58

Baderous
New Scratcher
Registered: 2011-04-14
Posts: 100+

Re: Smalltalk code for getting a method's content?

Greenatic wrote:

Baderous wrote:

"accessing method dictionary" is a clear name that points out where you have to search. After looking at that protocol's methods you find 2 of your interest: #sourceCodeAt: and #sourceMethodAt:. Choose the one that fits you better. To test, open an Inspector window on some morph, for intance a sprite, and then type:

Code:

self class sourceCodeAt: #attributeNames

Here I chose the #attributeNames method, but you can choose any other one.

Hmmm.  Nice find, Baderous.  Maybe I'm doing something wrong though, but it only seems to work on instance methods.  For example, what you posted works, but on a sprite, the following code returns the error "key not found":

Code:

self class sourceCodeAt: #blockSpecs

Maybe I'm making some silly mistake though.  So you have any idea why it won't work?

Doing "self class" on a sprite returns the name of its class, ScratchSpriteMorph. So, calling those 2 methods on it will only work for instance methods. To access its class methods you have to call those methods on the class's class, which is "ScratchSpriteMorph class". So all you have to do is:

Code:

self class class sourceCodeAt: #blockSpecs

Offline

 

#8 2011-09-08 16:32:42

Greenatic
Scratcher
Registered: 2009-05-03
Posts: 1000+

Re: Smalltalk code for getting a method's content?

Baderous wrote:

Greenatic wrote:

Baderous wrote:

"accessing method dictionary" is a clear name that points out where you have to search. After looking at that protocol's methods you find 2 of your interest: #sourceCodeAt: and #sourceMethodAt:. Choose the one that fits you better. To test, open an Inspector window on some morph, for intance a sprite, and then type:

Code:

self class sourceCodeAt: #attributeNames

Here I chose the #attributeNames method, but you can choose any other one.

Hmmm.  Nice find, Baderous.  Maybe I'm doing something wrong though, but it only seems to work on instance methods.  For example, what you posted works, but on a sprite, the following code returns the error "key not found":

Code:

self class sourceCodeAt: #blockSpecs

Maybe I'm making some silly mistake though.  So you have any idea why it won't work?

Doing "self class" on a sprite returns the name of its class, ScratchSpriteMorph. So, calling those 2 methods on it will only work for instance methods. To access its class methods you have to call those methods on the class's class, which is "ScratchSpriteMorph class". So all you have to do is:

Code:

self class class sourceCodeAt: #blockSpecs

That works perfectly!  I think that's what scimonster needed...

Offline

 

#9 2011-09-08 21:56:52

cskidmsonic
Scratcher
Registered: 2011-01-22
Posts: 100+

Re: Smalltalk code for getting a method's content?

Nice find!


This signature is TDD's fault... yes, I had to.

Offline

 

#10 2011-09-09 02:48:30

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Smalltalk code for getting a method's content?

Yay! I found a way to have the blocks get in, but this looks like it should be useful for the uncoloredArgMorphFor:.  wink

Offline

 

Board footer