mix ins

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

mix ins

Joseph Alotta
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?

Sincerely,

Joe.



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: mix ins

Bert Freudenberg
On Tue, Aug 2, 2016 at 5:03 PM, Joseph Alotta <[hidden email]> 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 -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: mix ins

Joseph Alotta

> On Aug 2, 2016, at 9:38 AM, Bert Freudenberg [via Smalltalk] <[hidden email]> wrote:
>
> 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.

Bert,

I am not sure how to do this.  My project is called Books.  Is this what you mean?

Object subclass: #Array
        instanceVariableNames: ''
        classVariableNames: ''
        poolDictionaries: ''
        category: ‘*Books’


Sincerely,

Joe.


PS.  Thanks for the bit about the join methods and the changes.  :-)


Reply | Threaded
Open this post in threaded view
|

Re: mix ins

Joseph Alotta
In reply to this post by Bert Freudenberg
Bert,

I don’t have these methods.  I get an error.  See attached:

Sincerely,

Joe.


On Aug 2, 2016, at 9:38 AM, Bert Freudenberg [via Smalltalk] <[hidden email]> wrote:

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'



Reply | Threaded
Open this post in threaded view
|

Re: mix ins

Bert Freudenberg
On Tue, Aug 2, 2016 at 5:48 PM, Joseph Alotta <[hidden email]> wrote:
Bert,

I don’t have these methods.  I get an error.  See attached:


Are you using Squeak 5.0?

- Bert -

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: mix ins

Bert Freudenberg
In reply to this post by Joseph Alotta
On Tue, Aug 2, 2016 at 5:39 PM, Joseph Alotta <[hidden email]> wrote:

> On Aug 2, 2016, at 9:38 AM, Bert Freudenberg [via Smalltalk] <[hidden email]> wrote:
>
> 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.

Bert,

I am not sure how to do this.  My project is called Books.

I meant a method category (a.k.a. protocol). Right-click in Array's method categories list and add a new category named *books. Then select it and put your methods there. You can also use drag-and-drop to move a method to the right category.
 
Inline image 1

- Bert -

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: mix ins

Joseph Alotta
In reply to this post by Bert Freudenberg
4.3

What is the procedure for upgrading?  Should I file-out the classes, download the new version and then file-in?

Sincerely,

Joe.



> On Aug 2, 2016, at 1:31 PM, Bert Freudenberg [via Smalltalk] <[hidden email]> wrote:
>
> On Tue, Aug 2, 2016 at 5:48 PM, Joseph Alotta <[hidden email]> wrote:
> Bert,
>
> I don’t have these methods.  I get an error.  See attached:
>
>
> Are you using Squeak 5.0?
>
> - Bert -
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>
> If you reply to this email, your message will be added to the discussion below:
> http://forum.world.st/mix-ins-tp4909171p4909199.html
> To start a new topic under Squeak - Beginners, email [hidden email]
> To unsubscribe from Squeak - Beginners, click here.
> NAML

Reply | Threaded
Open this post in threaded view
|

Re: mix ins

Bert Freudenberg
On Tue, Aug 2, 2016 at 8:55 PM, Joseph Alotta <[hidden email]> wrote:
4.3

What is the procedure for upgrading?  Should I file-out the classes, download the new version and then file-in?

That would be one simple way to do it, yes.

OTOH if you're happy with 4.3 there is no need to upgrade - just add the methods you need to your image.

- Bert - 


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: mix ins

Joseph Alotta
Hi Bert,

I downloaded Squeak 5.0.  Seems to work well.  But I have a few issues:

1.  How do I make the system font bigger?

2.  How do I make the flaps disappear?

Sincerely,

Joe.



Reply | Threaded
Open this post in threaded view
|

Re: mix ins

Joseph Alotta
In reply to this post by Bert Freudenberg
I was able to get everything working fine on 5.0.  I like it better than 4.3.  I think you guys have done a great job!!

Thank you.

Joe.





> On Aug 2, 2016, at 8:07 PM, Joseph Alotta <[hidden email]> wrote:
>
> Hi Bert,
>
> I downloaded Squeak 5.0.  Seems to work well.  But I have a few issues:
>
> 1.  How do I make the system font bigger?
>
> 2.  How do I make the flaps disappear?
>
> Sincerely,
>
> Joe.
>
>
>