On Fri, Apr 9, 2010 at 4:31 PM, Joao Carlos <m...@joao-carlos.com> wrote: > I'll admit mine was a bad example. However, the most common (only?) > use case I could see for it is in fact with #send. > > Your idea for filtering is great, by the way.
Instead of send you can use instance_eval. Where you'd do something like obj.send(x? ? :self : :other) you can do obj.instance_eval { x? ? self : other } Cheers -foca > > On Apr 9, 9:19 pm, Mislav Marohnić <mislav.maroh...@gmail.com> wrote: >> On Fri, Apr 9, 2010 at 13:26, Joao Carlos <m...@joao-carlos.com> wrote: >> >> > filter = %w(with_votes without_votes).include?(params[:filter]) ? >> > params[:filter].to_sym : :self >> > Idea.published.send(filter).all >> >> I wouldn't call this use case where it would be "extremely handy". Here is >> better code for it: >> >> filters = Idea.scopes.keys & Array.wrap(params[:filter]).map(&:to_sym) >> >> records = filters.inject(Idea.published) { |model, name| >> model.send(name) }.all >> >> First bonus feature is that it knows about all your named scopes, you don't >> have to specify them in the controller (business logic in your controller = >> wrong). >> >> Another bonus feature — multiple filters can be given: >> >> GET /resource?filter[]=foo&filter[]=bar >> >> Third bonus feature is that we didn't need to extend the language with a >> method that doesn't do absolutely anything. >> (Even `tap` does something and is incredibly useful.) > > -- > 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-c...@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-core+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. > > -- 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-c...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.