Chauk-Mean P
2007-Sep-20 21:17 UTC
[wxruby-development] Bug? in wxSugar enumerable_controls.rb
Hi, I wanted to use wxSugar enumerable features to add a method for returning checked items'' index to the CheckListBox class : require ''wx_sugar/version'' require ''wx_sugar/wx_classes/control_with_items'' require ''wx_sugar/wx_classes/listctrl'' class Wx::CheckListBox def checked_items # use of find_all method from Enumerable items_index = find_all { |i| checked?(i) } end end The problem I had is that checked_items raises an exception. In fact, the find_all method uses the each method defined in ''wx_sugar/wx_classes/enumerable_controls.rb'' as following : def each(&block) last = get_count - 1 case block.arity when 1 0.upto(last) { | i | yield i } when 2 0.upto(last) { | i | yield i, get_string(i) } when 3 0.upto(last) { | i | yield i, get_string(i), get_item_data(i) } else raise ArgumentError, "Invalid number of block parameters" end end This method works well if used directly. The problem is that methods from Enumerable like find_all, collect ... calls the each method providing a block with an arity of -1 ! (I''m running Ruby 1.8.5p12 on Win32). A workaround for this problem is to add -1 as an acceptable arity : ... when 1, -1 0.upto(last) { | i | yield i } ... Given the Pickaxe book, an arity of -1 for a block seems to be valid. So I don''t know if it''s a Ruby bug or a wxSugar bug. Any clarification is welcome. Cheers. Chauk-Mean.
Chauk-Mean P
2007-Sep-20 21:44 UTC
[wxruby-development] Bug? in wxSugar enumerable_controls.rb
2007/9/20, Chauk-Mean P <chauk.mean at gmail.com>:> > Given the Pickaxe book, an arity of -1 for a block seems to be valid. > So I don''t know if it''s a Ruby bug or a wxSugar bug. >It is certainly a bug in wxSugar. An arity of -1 indicates a block that takes any number of arguments : lambda { | | }.arity # => 0 lambda { |i| }.arity # => 1 lambda { }.arity # => -1 lambda { |*n| }.arity # => -1 lambda { |i, *n| }.arity # => -2 Cheers, Chauk-Mean.
Alex Fenton
2007-Sep-20 21:46 UTC
[wxruby-development] Bug? in wxSugar enumerable_controls.rb
Chauk-Mean P wrote:> def each(&block) > last = get_count - 1 > case block.arity > when 1 > 0.upto(last) { | i | yield i } > when 2 > 0.upto(last) { | i | yield i, get_string(i) } > when 3 > 0.upto(last) { | i | yield i, get_string(i), get_item_data(i) } > else > raise ArgumentError, "Invalid number of block parameters" > end > end > > This method works well if used directly. > The problem is that methods from Enumerable like find_all, collect ... > calls the each method providing a block with an arity of -1 ! > (I''m running Ruby 1.8.5p12 on Win32). >Sounds like a wxSugar bug since -1 is a valid arity - although perhaps how Enumerable works has changed to show this up. Following recent discussions I''m inclined to think that an ''each'' method is useful enough in list-like classes to make it worth adding to core so plan to do this for 1.9.2 (I see QtRuby has done something similar recently). But I think the current implementation is a bit over-elaborate, so I''ll make it just pass a single block parameter in all cases, and let the user call get_string or get_item_data if desired. The implementation will be something like def each 0.upto(count - 1) { | i | yield i } end Thanks for the report &c cheers alex
Chauk-Mean P
2007-Sep-20 22:22 UTC
[wxruby-development] Bug? in wxSugar enumerable_controls.rb
2007/9/20, Alex Fenton <alex at pressure.to>:> Following recent discussions I''m inclined to think that an ''each'' method > is useful enough in list-like classes to make it worth adding to core so > plan to do this for 1.9.2 (I see QtRuby has done something similar > recently).That''s great.> But I think the current implementation is a bit over-elaborate, so I''ll > make it just pass a single block parameter in all cases, and let the > user call get_string or get_item_data if desired. The implementation > will be something like > > def each > 0.upto(count - 1) { | i | yield i } > endSimplicity is better :-)> Thanks for the report &cYou''re welcome. Cheers Chauk-Mean.
Maybe Matching Threads
- [940] trunk/wxsugar/lib/wx_sugar/wx_classes/treectrl.rb: Align #traverse usage with #each in enumerable_controls.rb
- [938] trunk/wxsugar/lib/wx_sugar/wx_classes: Enable enumerable_controls for ListCtrl and ControlWithItems family
- [937] trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb: Initial commit of enumerable_controls.rb
- [945] trunk/wxsugar/lib/wx_sugar.rb: Add note about enumerable_controls, reorder list of extensions
- [943] trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb: Fix bug with false positives from #find_string, bounds-checking for