I have this ordered Core Data to-many relation named "operations" that I want to iterate over. I wrote: self.operations.each { | operation | operation.doSomething } However this fails because self.operations returns an NSOrderedSet and NSOrderedSet doesn't have an 'each' method. I was able to use the 'enumerateObjectsUsingBlock' method of NSOrderedSet which is working fine. My question is: what would be the MacRuby way to add an 'each' method to NSOrderedSet? Thanks, Jean-Denis
Give it some ruby love! I found this somewhere in the interwebs to iterate over an NSIndexSet, which should be similar. https://gist.github.com/1370277 dw On Wed, Nov 16, 2011 at 3:11 AM, Jean-Denis MUYS <jdmuys@kleegroup.com>wrote:
I have this ordered Core Data to-many relation named "operations" that I want to iterate over. I wrote:
self.operations.each { | operation | operation.doSomething }
However this fails because self.operations returns an NSOrderedSet and NSOrderedSet doesn't have an 'each' method.
I was able to use the 'enumerateObjectsUsingBlock' method of NSOrderedSet which is working fine.
My question is: what would be the MacRuby way to add an 'each' method to NSOrderedSet?
Thanks,
Jean-Denis
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
On 17/11/2011, at 4:07 AM, Daniel Westendorf wrote:
Give it some ruby love! I found this somewhere in the interwebs to iterate over an NSIndexSet, which should be similar. https://gist.github.com/1370277
And while you're at it, check out Bean... https://github.com/dj2/Bean Lots more helpful little snippets. Henry
this should work, you can still use enumerateIndexesUsingBlock! framework 'Foundation' class NSIndexSet def each return self.to_enum unless block_given? self.enumerateIndexesUsingBlock -> idx, stop { yield idx } end end indexes = NSIndexSet.indexSetWithIndexesInRange NSMakeRange(0, 300) indexes.each do |idx| puts idx end
The Cbjective-C way to handle this would be to put a category that provided an `each` method on NSOrderedSet. Then when you called `each` it would just work. Ruby has a similar functionality but I can't remember right now what it's called. Using a category would be optimal in the case of Core Data because in some instances you can actually evoke a method as part of a key path when sending Key-Value messages. Shannon On Nov 16, 2011, at 4:11 AM, Jean-Denis MUYS wrote:
I have this ordered Core Data to-many relation named "operations" that I want to iterate over. I wrote:
self.operations.each { | operation | operation.doSomething }
However this fails because self.operations returns an NSOrderedSet and NSOrderedSet doesn't have an 'each' method.
I was able to use the 'enumerateObjectsUsingBlock' method of NSOrderedSet which is working fine.
My question is: what would be the MacRuby way to add an 'each' method to NSOrderedSet?
Thanks,
Jean-Denis
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
If you didn't want to use a category, you could do: orderedSet = NSOrderedSet.orderedSetWithArray(["1", "2", "3"]) orderedSet.class.send(:define_method, :each) do self.array.each do |item| yield item end end orderedSet.each do |item| puts item end This method also has the advantage of capturing local scope, if needed. hth kam On Nov 17, 2011, at 8:22 AM, techzen wrote:
The Cbjective-C way to handle this would be to put a category that provided an `each` method on NSOrderedSet. Then when you called `each` it would just work. Ruby has a similar functionality but I can't remember right now what it's called.
Using a category would be optimal in the case of Core Data because in some instances you can actually evoke a method as part of a key path when sending Key-Value messages.
Shannon
On Nov 16, 2011, at 4:11 AM, Jean-Denis MUYS wrote:
I have this ordered Core Data to-many relation named "operations" that I want to iterate over. I wrote:
self.operations.each { | operation | operation.doSomething }
However this fails because self.operations returns an NSOrderedSet and NSOrderedSet doesn't have an 'each' method.
I was able to use the 'enumerateObjectsUsingBlock' method of NSOrderedSet which is working fine.
My question is: what would be the MacRuby way to add an 'each' method to NSOrderedSet?
Thanks,
Jean-Denis
_______________________________________________ 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
you could also create a module and mix it in with the objects you want have the custom methods defined in. - Matt On Thu, Nov 17, 2011 at 3:21 PM, Kam Dahlin <haxie1@me.com> wrote:
If you didn't want to use a category, you could do:
orderedSet = NSOrderedSet.orderedSetWithArray(["1", "2", "3"]) orderedSet.class.send(:define_method, :each) do self.array.each do |item| yield item end end
orderedSet.each do |item| puts item end
This method also has the advantage of capturing local scope, if needed.
hth kam
On Nov 17, 2011, at 8:22 AM, techzen wrote:
The Cbjective-C way to handle this would be to put a category that provided an `each` method on NSOrderedSet. Then when you called `each` it would just work. Ruby has a similar functionality but I can't remember right now what it's called.
Using a category would be optimal in the case of Core Data because in some instances you can actually evoke a method as part of a key path when sending Key-Value messages.
Shannon
On Nov 16, 2011, at 4:11 AM, Jean-Denis MUYS wrote:
I have this ordered Core Data to-many relation named "operations" that I want to iterate over. I wrote:
self.operations.each { | operation | operation.doSomething }
However this fails because self.operations returns an NSOrderedSet and NSOrderedSet doesn't have an 'each' method.
I was able to use the 'enumerateObjectsUsingBlock' method of NSOrderedSet which is working fine.
My question is: what would be the MacRuby way to add an 'each' method to NSOrderedSet?
Thanks,
Jean-Denis
_______________________________________________ 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
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
participants (7)
-
Daniel Westendorf
-
Henry Maddocks
-
Jean-Denis MUYS
-
Kam Dahlin
-
Mateus Armando
-
Matt Aimonetti
-
techzen