[MacRuby-devel] Monkey patching Objective-C classes

Laurent Sansonetti lsansonetti at apple.com
Fri Aug 21 11:47:31 PDT 2009


On Aug 21, 2009, at 9:33 AM, Clay Bridges wrote:

> I'm using MacRuby to test some of my ObjC classes. I was wondering if
> there was a canonical way to monkey patch these classes.

Just open them as you would do in Ruby.

> Consider, the following where Cell is an ObjC class:
>
> irb(main):001:0> Cell.object_id
> => 4387749088
> irb(main):002:0> class Cell
> irb(main):003:1>   def whee
> irb(main):004:2>     p 'whee'
> irb(main):005:2>   end
> irb(main):006:1> end
> => nil
> irb(main):007:0> Cell.object_id
> => 4388625216
>
> I would expect the object_id to stay the same, e.g. using pure  
> MacRuby:
>
> irb(main):010:0> class Bar
> irb(main):011:1> end
> => nil
> irb(main):012:0> Bar.object_id
> => 4298834304
> irb(main):013:0> class Bar
> irb(main):014:1>   def drink
> irb(main):015:2>     p 'tasty!'
> irb(main):016:2>   end
> irb(main):017:1> end
> => nil
> irb(main):018:0>  Bar.object_id
> => 4298834304

It's indeed strange. I can't reproduce this on the command-line using  
NSPredicate.

$ ./miniruby -e "p NSPredicate.object_id; class NSPredicate; def  
hey;end; end; p NSPredicate.object_id"
140735073102888
140735073102888

Normally the class shouldn't change if it's just re-opened.

Do you see the #whee method in Objective-C land once you patch it from  
Ruby?

Laurent


More information about the MacRuby-devel mailing list