[Newbies] mix ins

Bert Freudenberg bert at freudenbergs.de
Tue Aug 2 15:23:00 UTC 2016


On Tue, Aug 2, 2016 at 5:03 PM, Joseph Alotta <joseph.alotta at gmail.com>
wrote:

> Greetings,
>
> I want to add a method to the Array class.
>
> #(‘abc’ ‘def’) join =>  ‘abcdef’
>
> #(‘abc’ ‘def’) join: $/ => ‘abc/def’
>
> I am afraid that if I add it to Array directly, it will get lost in a new
> update.  I want to keep it in my project area.
>
> In ruby, this is called a “mix in”.
>
> How can I do this?
>

Firstly, these methods exist already.  They are defined in
SequencableCollection, a  superclass of Array, so Array inherits them:

#('abc' 'def') join => 'abcdef'

#('abc' 'def') joinSeparatedBy: '/' => 'abc/def'

Secondly, if you wanted to add these (or other methods), they would not be
lost by updating. That is, unless the update specifically includes a method
of the same name in the same class. Updating only adds and removes methods,
it doesn't "reset" the whole class.

Thirdly, to keep these "extension methods" in your own package, put them in
a method category that starts with an asterisk followed by your package
name. That is, if your Monticello package is named "Foo-Bar", then put your
"mix in" Array methods into the category "*foo-bar" which will mark them as
belonging to your package, not the package the Array class is in. The
extension methods will be stored and loaded with your package.

Have fun!

- Bert -
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20160802/2b154da1/attachment.htm


More information about the Beginners mailing list