[Rails-core] Re: ActiveSupport: alternative Block execution on empty Enumerable

2009-08-06 Thread Ryan Angilly
... or perhaps even or_if_blank. :-) Ryan On Thu, Aug 6, 2009 at 11:02 AM, John W. Long wrote: > > On Aug 6, 2009, at 4:10 AM, Paul wrote: > > module Enumerable > > def else(&block) > >self.respond_to?('empty?') && self.empty? ? yield : self > > end > > end > > > Wouldn't this method b

[Rails-core] Re: ActiveSupport: alternative Block execution on empty Enumerable

2009-08-06 Thread John W. Long
On Aug 6, 2009, at 4:10 AM, Paul wrote: > module Enumerable > def else(&block) >self.respond_to?('empty?') && self.empty? ? yield : self > end > end Wouldn't this method be better named "ifempty"? If you call it "else" it makes me wonder where the other part of the clause is? Certainly i

[Rails-core] Re: ActiveSupport: alternative Block execution on empty Enumerable

2009-08-06 Thread Paul
much better - thank you! --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email

[Rails-core] Re: ActiveSupport: alternative Block execution on empty Enumerable

2009-08-06 Thread Luca Guidi
Just updated the benchmark (http://gist.github.com/163274), tried to solve the issue using #any? instead of #try, but isn't fast for filled arrays: module Enumerable def else not any? ? yield : self # don't try this at home end end Luca -- lucaguidi.com twitter.com/jodosha --~--~

[Rails-core] Re: ActiveSupport: alternative Block execution on empty Enumerable

2009-08-06 Thread Mike Gunderloy
Ticket: https://rails.lighthouseapp.com/projects/8994/tickets/1950 Mike On Aug 6, 2009, at 7:50 AM, Eloy Duran wrote: > Yeah creating a proc every time isn't needed and much cleaner this > way. I do think that it's better to use #blank? as was used in > Ryan's patch. Still waiting for that t

[Rails-core] Re: ActiveSupport: alternative Block execution on empty Enumerable

2009-08-06 Thread Eloy Duran
Ugh, scrap that, I need sleep. On Aug 6, 2009, at 2:50 PM, Eloy Duran wrote: > I do think that it's better to use #blank? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to th

[Rails-core] Re: ActiveSupport: alternative Block execution on empty Enumerable

2009-08-06 Thread Eloy Duran
Yeah creating a proc every time isn't needed and much cleaner this way. I do think that it's better to use #blank? as was used in Ryan's patch. Still waiting for that ticket :) Eloy On Aug 6, 2009, at 2:44 PM, Ryan Angilly wrote: > Similar discussion from an older ticket: > https://rails.li

[Rails-core] Re: ActiveSupport: alternative Block execution on empty Enumerable

2009-08-06 Thread Ryan Angilly
Similar discussion from an older ticket: https://rails.lighthouseapp.com/projects/8994/tickets/1815-patch-add-or_if_blank-to-object On Thu, Aug 6, 2009 at 8:38 AM, Luca Guidi wrote: > What about using Object#try and avoiding the explicit &block argument?This > avoid to instantiate a Proc object

[Rails-core] Re: ActiveSupport: alternative Block execution on empty Enumerable

2009-08-06 Thread Luca Guidi
What about using Object#try and avoiding the explicit &block argument?This avoid to instantiate a Proc object *for each* method execution, if any block isn't passed and the enumerable is empty the VM will raise a LocalJumpError. module Enumerable def else self.try(:empty?) ? yield : self e

[Rails-core] Re: ActiveSupport: alternative Block execution on empty Enumerable

2009-08-06 Thread Eloy Duran
I like it. Is there a ticket and patch that I can verify and add my +1? Eloy On Aug 6, 2009, at 10:10 AM, Paul wrote: > > i made a suggestion for this within lighthouse. > > Pratik said: "This looks interesting. But I'm not sure if this pattern > belongs to the core or not. You should probably