Monkey patching Objective-C classes
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. 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 Thanks Clay Bridges
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
I've reproduced it using 0.4, for whatever that's worth. If you open a builtin class (e.g. NSSet) with the class keyword, the object_id changes. If you open it with class_eval, the object_id is unchanged. Best, Jeremy On Fri, Aug 21, 2009 at 11:47 AM, Laurent Sansonetti <lsansonetti@apple.com>wrote:
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
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
I see, I haven't tried with 0.4. Maybe the problem is fixed in trunk :-) Laurent On Aug 21, 2009, at 12:10 PM, Jeremy Voorhis wrote:
I've reproduced it using 0.4, for whatever that's worth. If you open a builtin class (e.g. NSSet) with the class keyword, the object_id changes. If you open it with class_eval, the object_id is unchanged.
Best,
Jeremy
On Fri, Aug 21, 2009 at 11:47 AM, Laurent Sansonetti <lsansonetti@apple.com
wrote: 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
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Forgive my caveman-programmer terminology, but it seems to overwrite the class symbol. Extending Laurent's example (kind of): gort:~ clay$ macruby -e "p NSPredicate.object_id; p NSPredicate.methods(false,true); class NSPredicate; def hey;end; end; p NSPredicate.object_id; p NSPredicate.methods(false,true)" 140735077844512 [:predicateWithValue, :predicateWithFormat:arguments:, :predicateWithFormat, :predicateWithFormat:argumentArray:, :newStringFrom:usingUnicodeTransforms:, :initialize] 4298735808 [:__new__, :new, :alloc] gort:~ clay$ macruby --version MacRuby version 0.4 (ruby 1.9.1) [universal-darwin9.5, x86_64] If it's fixed, it still might be worth a unit test. ;) Clay
On Aug 21, 2009, at 2:06 PM, Clay Bridges wrote:
Forgive my caveman-programmer terminology, but it seems to overwrite the class symbol. Extending Laurent's example (kind of):
gort:~ clay$ macruby -e "p NSPredicate.object_id; p NSPredicate.methods(false,true); class NSPredicate; def hey;end; end; p NSPredicate.object_id; p NSPredicate.methods(false,true)"
140735077844512 [:predicateWithValue, :predicateWithFormat:arguments:, :predicateWithFormat, :predicateWithFormat:argumentArray:, :newStringFrom:usingUnicodeTransforms:, :initialize] 4298735808 [:__new__, :new, :alloc]
gort:~ clay$ macruby --version MacRuby version 0.4 (ruby 1.9.1) [universal-darwin9.5, x86_64]
If it's fixed, it still might be worth a unit test. ;)
It seems to be fixed in trunk :-) Laurent
participants (3)
-
Clay Bridges
-
Jeremy Voorhis
-
Laurent Sansonetti