Easiest way to add new methods to kernel classes (from within a script)?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Easiest way to add new methods to kernel classes (from within a script)?

Rick Flower
Paolo,

I'd like to add >>asArrayOfSubstrings (found in the Smalltalk
by Example book) into the CharacterArray class but would like
it to remain within my script so that I don't modify the kernel
files directly.. What's the best way to do that?  I tried adding
it to the top of my script file like shown below but GST wasn't
too happy about it.. Do I need to convert over to the new style
syntax to do this sort of thing or something else?

#!/usr/local/bin/gst -f

ArrayedCollection subclass: #CharacterArray
        instanceVariableNames: ''
        classVariableNames: ''
        poolDictionaries: ''
        category: 'Collections-Text'!
       
    asArrayOfSubstrings [
    <category: 'converting'>
        | first last collection |
       
        collection := OrderedCollection new.
        last := 0.
        [first := self findFirst: [ :char | char isSeparator not] startingAt:
last + 1. first ~= 0]
                whileTrue:
                        [last := (self findFirst: [ :char | char isSeparator] startingAt:
first) - 1.
                        last < 0 ifTrue: [last := self size].
                        collection add: (self copyFrom: first to: last)].
        ^collection asArray
! !

.
.
.



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Easiest way to add new methods to kernel classes (from within a script)?

Rick Flower
On Thu, 25 Feb 2010 14:14:23 -0800, Rick Flower <[hidden email]>
wrote:

> Paolo,
>
> I'd like to add >>asArrayOfSubstrings (found in the Smalltalk
> by Example book) into the CharacterArray class but would like
> it to remain within my script so that I don't modify the kernel
> files directly.. What's the best way to do that?  I tried adding
> it to the top of my script file like shown below but GST wasn't
> too happy about it.. Do I need to convert over to the new style
> syntax to do this sort of thing or something else?
>
> #!/usr/local/bin/gst -f
>
> ArrayedCollection subclass: #CharacterArray
> instanceVariableNames: ''
>         classVariableNames: ''
>         poolDictionaries: ''
>         category: 'Collections-Text'!
>        
>     asArrayOfSubstrings [
>     <category: 'converting'>
> | first last collection |
>
> collection := OrderedCollection new.
> last := 0.
> [first := self findFirst: [ :char | char isSeparator not] startingAt:
> last + 1. first ~= 0]
> whileTrue:
> [last := (self findFirst: [ :char | char isSeparator] startingAt:
> first) - 1.
> last < 0 ifTrue: [last := self size].
> collection add: (self copyFrom: first to: last)].
> ^collection asArray
> ! !

Ok..I converted the above code into the new format (along
with the rest of my script) and the code above looks
something like :

CharacterArray extend [
    asArrayOfSubstrings [
    <category: 'converting'>
        | first last collection |
       
        collection := OrderedCollection new.
        last := 0.
        [first := self findFirst: [ :char | char isSeparator not] startingAt:
last + 1. first ~= 0]
                whileTrue:
                        [last := (self findFirst: [ :char | char isSeparator] startingAt:
first) - 1.
                        last < 0 ifTrue: [last := self size].
                        collection add: (self copyFrom: first to: last)].
        ^collection asArray
]

But that causes problems with any class definitions below this code.. with
errors like : invalid class body element.. I think I'm close..




_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Easiest way to add new methods to kernel classes (from within a script)?

Lee Duhem
In reply to this post by Rick Flower
On Fri, Feb 26, 2010 at 6:14 AM, Rick Flower <[hidden email]> wrote:
> Paolo,
>
> I'd like to add >>asArrayOfSubstrings (found in the Smalltalk
> by Example book) into the CharacterArray class but would like
> it to remain within my script so that I don't modify the kernel
> files directly.. What's the best way to do that?

You can use

someClass extend [
    yourMethod [
        ...
    ]
]

or

someClass class extend [
    yourClassMethod [
        ...
    ]
]

in your script. This is the way I used to do it.

lee


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Easiest way to add new methods to kernel classes (from within a script)?

Paolo Bonzini-2
In reply to this post by Rick Flower

> CharacterArray extend [
>      asArrayOfSubstrings [
>      <category: 'converting'>
> | first last collection |
>
> collection := OrderedCollection new.
> last := 0.
> [first := self findFirst: [ :char | char isSeparator not] startingAt:
> last + 1. first ~= 0]
> whileTrue:
> [last := (self findFirst: [ :char | char isSeparator] startingAt:
> first) - 1.
> last<  0 ifTrue: [last := self size].
> collection add: (self copyFrom: first to: last)].
> ^collection asArray

Missing closing bracket here:

     ]
> ]

Paolo


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Easiest way to add new methods to kernel classes (from within a script)?

Rick Flower
Thanks!  I'll give it a try!

-- Rick

On Feb 26, 2010, at 1:28 AM, Paolo Bonzini wrote:

>
>> CharacterArray extend [
>>     asArrayOfSubstrings [
>>     <category: 'converting'>
>> | first last collection |
>>
>> collection := OrderedCollection new.
>> last := 0.
>> [first := self findFirst: [ :char | char isSeparator not]  
>> startingAt:
>> last + 1. first ~= 0]
>> whileTrue:
>> [last := (self findFirst: [ :char | char isSeparator] startingAt:
>> first) - 1.
>> last<  0 ifTrue: [last := self size].
>> collection add: (self copyFrom: first to: last)].
>> ^collection asArray
>
> Missing closing bracket here:
>
>    ]
>> ]
>
> Paolo



_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk