The Ruby language supports the concept of "Open Classs" or that at
run-time a class can have methods added to it, or in general have it's definition modified. As I understand it, the idea for this originally came from Smalltalk. If so, how would I go about adding methods (in the workspace, for example, just to test things out)? Thanks alot. -Rich |
On Jan 15, 2006, at 9:17 PM, Rich wrote: > The Ruby language supports the concept of "Open Classs" or that at > run-time a class can have methods added to it, or in general have it's > definition modified. As I understand it, the idea for this originally > came from Smalltalk. If so, how would I go about adding methods (in > the workspace, for example, just to test things out)? Thanks alot. The short answer would be evaluate something like this in workspace: Object compile: 'one ^ 1'. The longer answer is that adding a method at runtime is the only way to create methods at all. When you accept a method in the browser it does something very similar to the message send above, sending a message to the class to compile a method. In Smalltalk, run time is all the time. Colin |
In reply to this post by Rich-18
Rich,
Sounds like a topic from Matz blog^^; Note that all classes are born with zero method, and methods are added later; "the concept of Open Class" is not something Smalltalk "supports", but it is what everything is built on. I guess you already know how to add a method from a System Browser. If you type some code in the code pane and choose "accept" from the menu, it is compiled and added to the selected class. What you can do here is to bring up the right-click menu in the code pane, and "inspect" the "accept" menu item via Alt-click on the menu item. (See the attachment browser.png). In the inspector, you can type a expression like: target perform: #accept orSendTo: (arguments at: 2) (See another attchment inspector.png) and choose "debug it" from the menu of the inspector. This expression is what is evaluated when you choose the menu item, and the debugger will show up to do the step execution of the expression. You can step through what the Browser does when you hit the menu item. Along the line, you'll hit one of a method of ClassDescription called #compile:blahblah. As Colin wrote, what happens is is equivalent of evaluating: Object compile: 'one ^ 1'. By the way, there is a great Squeak book by Umezawa-san written in Japanese. It covers from "3+4" to the module system (Monticello) and everything in 594 pages. Arguably, it is the most through reference on Squeak. http://www.amazon.co.jp/exec/obidos/ASIN/4883732037/qid=1137400045/sr=8-1/ref=sr_8_xs_ap_i1_xgl/503-9335076-7943901 -- Yoshiki At Sun, 15 Jan 2006 21:17:50 -0500, Rich wrote: > > The Ruby language supports the concept of "Open Classs" or that at > run-time a class can have methods added to it, or in general have it's > definition modified. As I understand it, the idea for this originally > came from Smalltalk. If so, how would I go about adding methods (in > the workspace, for example, just to test things out)? Thanks alot. > > -Rich > |
In reply to this post by Rich-18
The short answer, is don't add methods from a workspace, just open the
class browser, find the class you want, and add a method to it. Every class in the system is open for you to edit anytime you like. > -----Original Message----- > From: [hidden email] > [mailto:[hidden email]] On > Behalf Of Rich > Sent: Sunday, January 15, 2006 7:18 PM > To: [hidden email] > Subject: Beginner smalltalk question: Open Classes and Smalltalk > > The Ruby language supports the concept of "Open Classs" or > that at run-time a class can have methods added to it, or in > general have it's definition modified. As I understand it, > the idea for this originally came from Smalltalk. If so, how > would I go about adding methods (in the workspace, for > example, just to test things out)? Thanks alot. > > -Rich > > |
I see one main difference here with Ruby. When you add methods "at
runtime", eg. programatically, they stick around in the class definition. In ruby they are there only in the context of the current runtime. Smalltalk saves its runtime state at exit (unless you choose to not save the image when you exit). So, meta-programming has a bit of a different flavor in Smalltalk than in Ruby. On 1/16/06, Ramon Leon <[hidden email]> wrote: > The short answer, is don't add methods from a workspace, just open the > class browser, find the class you want, and add a method to it. Every > class in the system is open for you to edit anytime you like. > > > -----Original Message----- > > From: [hidden email] > > [mailto:[hidden email]] On > > Behalf Of Rich > > Sent: Sunday, January 15, 2006 7:18 PM > > To: [hidden email] > > Subject: Beginner smalltalk question: Open Classes and Smalltalk > > > > The Ruby language supports the concept of "Open Classs" or > > that at run-time a class can have methods added to it, or in > > general have it's definition modified. As I understand it, > > the idea for this originally came from Smalltalk. If so, how > > would I go about adding methods (in the workspace, for > > example, just to test things out)? Thanks alot. > > > > -Rich > > > > > > -- Jason Rogers "I am crucified with Christ: nevertheless I live; yet not I, but Christ liveth in me: and the life which I now live in the flesh I live by the faith of the Son of God, who loved me, and gave himself for me." Galatians 2:20 |
I guess that's what I'm getting at. It'd be nice to play around with
internals without having the Smalltalk image remember what I did. I suppose it wouldn't be much trouble to confine my "playing around with" within a specific "junk" project. -Rich On 1/16/06, Jason Rogers <[hidden email]> wrote: > I see one main difference here with Ruby. When you add methods "at > runtime", eg. programatically, they stick around in the class > definition. In ruby they are there only in the context of the current > runtime. Smalltalk saves its runtime state at exit (unless you choose > to not save the image when you exit). So, meta-programming has a bit > of a different flavor in Smalltalk than in Ruby. > > On 1/16/06, Ramon Leon <[hidden email]> wrote: > > The short answer, is don't add methods from a workspace, just open the > > class browser, find the class you want, and add a method to it. Every > > class in the system is open for you to edit anytime you like. > > > > > -----Original Message----- > > > From: [hidden email] > > > [mailto:[hidden email]] On > > > Behalf Of Rich > > > Sent: Sunday, January 15, 2006 7:18 PM > > > To: [hidden email] > > > Subject: Beginner smalltalk question: Open Classes and Smalltalk > > > > > > The Ruby language supports the concept of "Open Classs" or > > > that at run-time a class can have methods added to it, or in > > > general have it's definition modified. As I understand it, > > > the idea for this originally came from Smalltalk. If so, how > > > would I go about adding methods (in the workspace, for > > > example, just to test things out)? Thanks alot. > > > > > > -Rich > > > > > > > > > > > > > -- > Jason Rogers > > "I am crucified with Christ: nevertheless I live; yet not I, > but Christ liveth in me: and the life which I now live in > the flesh I live by the faith of the Son of God, who loved > me, and gave himself for me." > Galatians 2:20 > > |
Rich wrote:
> I guess that's what I'm getting at. It'd be nice to play around with > internals without having the Smalltalk image remember what I did. I > suppose it wouldn't be much trouble to confine my "playing around > with" within a specific "junk" project. > my 2 cents: simply copy your image and changes file as junk.image and junk.changes and you can play around as much as you want with it... Stef |
In reply to this post by Rich-18
2006/1/16, Rich <[hidden email]>:
> The Ruby language supports the concept of "Open Classs" or that at > run-time a class can have methods added to it, or in general have it's > definition modified. As I understand it, the idea for this originally > came from Smalltalk. If so, how would I go about adding methods (in > the workspace, for example, just to test things out)? Thanks alot. I think the closest thing open classes map to are class extensions (please correct me if I'm wrong). A class extension is a method that belongs to a different package than the class for which it is defined. So for example if you want to add a method to String, but it should belong to MyPackage. The way you do that in Squeak is create the method in a *MyPackage category in String. |
Free forum by Nabble | Edit this page |