Categories like in Objective-C

Hola,

Objective-C has categories. They allow you to extend a class like this:

@interface NSArray (ArrayAdditions)

  • (void) randomize;
    @end

@implementation NSArray (ArrayAdditions)

  • (void) randomize {
    … this executes as if it is a method of NSArray …
    }
    @end

This example adds a randomize method to the existing NSArray class.

The Category is active the moment it is loaded into the runtime.

Is this also possible in Ruby? I know about Class.extend and Mixins, but what I
am looking for is the auto-loading.

S.

Hello Stefan,

Stefan Arentz wrote:

Hola,

Objective-C has categories. They allow you to extend a class like this:

@interface NSArray (ArrayAdditions)

  • (void) randomize;
    @end

@implementation NSArray (ArrayAdditions)

  • (void) randomize {
    … this executes as if it is a method of NSArray …
    }
    @end

This example adds a randomize method to the existing NSArray class.

The Category is active the moment it is loaded into the runtime.

Is this also possible in Ruby? I know about Class.extend and Mixins, but what I
am looking for is the auto-loading.

S.

class Array
def randomize
# insert your code here
end
end

a = Array.new
a.randomize

:wink:

Hope that helps,

···


Laurent

Laurent Sansonetti laurent@datarescue.be writes:

class Array
def randomize
# insert your code here
end
end

a = Array.new
a.randomize

:wink:

Hmpf. That easy huh?

Thanks :slight_smile:

S.

This is Ruby we’re talking about here. Nobody makes you crawl over broken
glass just to get things done. :slight_smile:

Jason Creighton

···

On 18 Jun 2003 19:27:30 +0200 Stefan Arentz stefan.arentz@soze.com wrote:

Laurent Sansonetti laurent@datarescue.be writes:

class Array
def randomize
# insert your code here
end
end

a = Array.new
a.randomize

:wink:

Hmpf. That easy huh?