Hello, I literally just installed squeak about an hour ago. After looking at some tutorials and playing around I have a basic understanding of the environment. Im now trying to figure out how to add a method to the smallinteger class. I have found some examples but they are a little too complex for me to understand.....sorrry im being dumb...... the method I would like to add would be called larger and would return larger of receiver and parameter. Could someone please walk me through this step by step.
for example.
10 larger: 6 evaluates to 10
thanks for your help in advance.
Go to the SmallInteger class in the browser and look at any method.
Type over all the text in the pane, replaceing the old method body with the new one you want to write. Select "accept" from the menu (or hit the keyboard shortcut). Note that you did NOT delete the old method, you can still see it, but instead there is now a new method.
In general, it is better to avoid putting code in old classes, but one of the nice things about Smalltalk is that it is easy to change old classes. And in any case, you are clearly just trying to figure out how to put a new method in an existing class, and this is how.
-Ralph Johnson
I was just looking at the method
gcd: anInteger "See SmallInteger (Integer) | gcd:" | n m | n := self. m := anInteger. [n = 0] whileFalse: [n := m \ (m := n)]. ^ m abs
the best thing i can think to do would be ......would this work
larger: anInteger | n m | n := self. m := anInteger. n > m [^n]. m>n [^m].
Hi,
n>I have found some examples but they are a little too n> complex for me to understand.....sorrry im being dumb...... the method I n> would like to add would be called larger and would return larger of receiver n> and parameter. Could someone please walk me through this step by step.
from the Menu bar, "Tools" get a Browser. Right click on the top left pane, choose "find class" type in "Integer" and select "Integer". I guess that's what you meant by "basic understanding of the environment" but just in case...
In the third top pane click on "comparing", this is the proper category for your lager: method.
In the bottom pane edit the template to:
larger: anInteger "return the larger of self and anInteger 6 larger: 10 " anInteger > self ifTrue: [^anInteger] ifFalse: [^self]
Right mouse click and choose "accept"
Then select the line 6 larger: 10, right click and select "print it".
n> for example. n> 10 larger: 6 n> evaluates to 10
Then open a Workspace from the menubar and try your example.
HTH
Cheers,
Herbert
Thank you so much that was just what i was looking for
"Herbert" == Herbert König herbertkoenig@gmx.net writes:
Herbert> larger: anInteger Herbert> "return the larger of self and anInteger Herbert> 6 larger: 10 Herbert> " Herbert> anInteger > self ifTrue: [^anInteger] ifFalse: [^self]
or even
larger: anInteger ^self max: anInteger
Code re-use, bay-bee.
And to see what defines "max:", open a "method finder", type "max:" in the proper window, and you can see the three classes that define "max:". The one closest to yours is "Magnitude", which is anything that has a linear comparability.
Hi Randal,
RLS> larger: anInteger RLS> ^self max: anInteger RLS> Code re-use, bay-bee.
true but couldn't resist to throw blocks at a newbie myself :-))
On 23.04.2010, at 23:19, Herbert König wrote:
Hi Randal,
RLS> larger: anInteger RLS> ^self max: anInteger RLS> Code re-use, bay-bee.
true but couldn't resist to throw blocks at a newbie myself :-))
Sure, but two blocks are just too scary ;) Much more fun to figure out why this works:
larger: anInteger anInteger > self ifTrue: [^ anInteger]
- Bert -
OK, Bert is having a little fun. Certainly good to learn that self is the default value.
But I wouldn't think of this as idiomatic smalltalk style.
I would prefer to follow "Interesting Return Value" [Beck]. I also factored out the return, since that is the same in both branches.
larger: anInteger ^ self > anInteger ifTrue: [self] ifFalse: [anInteger]
On Fri, Apr 23, 2010 at 4:39 PM, Bert Freudenberg bert@freudenbergs.dewrote:
On 23.04.2010, at 23:19, Herbert König wrote:
Hi Randal,
RLS> larger: anInteger RLS> ^self max: anInteger RLS> Code re-use, bay-bee.
true but couldn't resist to throw blocks at a newbie myself :-))
Sure, but two blocks are just too scary ;) Much more fun to figure out why this works:
larger: anInteger anInteger > self ifTrue: [^ anInteger]
- Bert -
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
beginners@lists.squeakfoundation.org