Why not rewrite that as: if @admins_only @users.select(&:admin?).sort else @users.sort end
or even @users = @users.select(&:admin?) if @admins_only @users.sort I think that is infinitely easier to read and understand (it took me a while to figure out what the hell your code was doing). Just because you *can* make everything into a one-liner in Ruby, doesn't mean you *should*. Also from the naming of the variables it sounds like you are doing this in the controller, that's the wrong place to do this sort of thing, imho. Put it in the model instead! @users.readable_by(@user) This clearly communicates what you are trying to achieve. /Jonas On Fri, Apr 16, 2010 at 1:22 PM, Evgeniy Dolzhenko <[email protected]> wrote: > +1 > > Also had the need for something like this in the past. > > Object#presence won't cut it when you're short-circuiting some part > of computation inside of the chained calls, for example this > > @users.send(@admins_only ? :select : :presence, &:admin?).sort > > will fail when @users array is blank. > > Also isn't this function sort of OO version of > http://en.wikipedia.org/wiki/Identity_function ? > > Btw. the use cases like this could be made even simpler by > adding/using Object#send_if > > @users.send_if(@admins_only, :select, &:admin?).sort > > And it looks like people have been doing this for quite a while > already :) > > http://www.google.com/codesearch?q=lang%3Aruby+def%5Cssend_if > > On Apr 11, 1:17 am, Josh Susser <[email protected]> wrote: >> On Apr 9, 2010, at 4:26 AM, Joao Carlos wrote: >> >> >> >> >> >> > I just came across a situation where it would be extremely handy to >> > have a method that returns the receiver. >> >> > Imagine the following example: >> >> > filter = %w(with_votes without_votes).include?(params[:filter]) ? >> > params[:filter].to_sym : :self >> > Idea.published.send(filter).all >> >> > The implementation would be pretty simple: >> >> > class Object >> > def self >> > self >> > end >> > end >> >> > Should this be a part of Rails? >> >> (somehow managed to forget to hit send on this one yesterday. sigh.) >> >> Smalltalk has a method Object#yourself which does just that. The most >> common use case is with cascaded message sends, but Ruby doesn't have >> those. I've wanted #yourself in Ruby now and then too. Since Matz >> was very familiar with Smalltalk when he created Ruby I assume he >> didn't think it was necessary, but it's one of the questions I always >> want to ask him but forget to when I get the chance. >> >> -- >> Josh Susserhttp://blog.hasmanythrough.com > > -- > 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 [email protected]. > To unsubscribe from this group, send email to > [email protected]. > 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
