I originally brought this up in: https://github.com/rails/rails/issues/9067 Rails paved the way for Object#tap and Object#try...I''d like to propose Object#tap_if and its counterpart,Object#tap_unless. I''ve been following 37signals conventions of tapping variables in the views: <% account.owner.tap do |user| %> ... <% end %> But, I find myself having to do this a lot... <% account.owner.tap do |user| %> <% if user %> ... <% end %> <% end %> It would be great if we could do... <% account.owner.tap_if(:present?) do |user| %> ... <% end %> <% account.users.tap_if(:any?) do |user| %> ... <% end %> The block would only yield if the method evals to true. Carlos mentioned that you can add an "if account.owner.present?" at the end... But there are times when the account.owner (or something else) call is expensive and you don''t want to call it twice. Any feedback would be much appreciated. Thanks! -- 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 to rubyonrails-core+unsubscribe@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
What''s wrong with... <% if user = account.owner %> ... <% end %> or <% if users = account.users.presence %> ... <% end %> ? On Fri, Jan 25, 2013 at 3:17 PM, Ngan <nganpham@gmail.com> wrote:> I originally brought this up in: > https://github.com/rails/rails/issues/9067 > > Rails paved the way for Object#tap and Object#try...I''d like to propose > Object#tap_if and its counterpart,Object#tap_unless. > > I''ve been following 37signals conventions of tapping variables in the > views: > > <% account.owner.tap do |user| %> > ... > <% end %> > > But, I find myself having to do this a lot... > > <% account.owner.tap do |user| %> > <% if user %> > ... > <% end %> > <% end %> > > It would be great if we could do... > > <% account.owner.tap_if(:present?) do |user| %> > ... > <% end %> > > <% account.users.tap_if(:any?) do |user| %> > ... > <% end %> > > The block would only yield if the method evals to true. > Carlos mentioned that you can add an "if account.owner.present?" at the > end... > But there are times when the account.owner (or something else) call is > expensive and you don''t want to call it twice. > > Any feedback would be much appreciated. Thanks! > > -- > 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 to > rubyonrails-core+unsubscribe@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- 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 to rubyonrails-core+unsubscribe@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Object#try takes a block. It''ll probably take care of most simple cases for you: <% account.owner.try do |user| %> <!-- This is shown only if account.owner is not nil? --> <% end %> On Fri, Jan 25, 2013 at 1:17 PM, Ngan <nganpham@gmail.com> wrote:> I originally brought this up in: > https://github.com/rails/rails/issues/9067 > > Rails paved the way for Object#tap and Object#try...I''d like to propose > Object#tap_if and its counterpart,Object#tap_unless. > > I''ve been following 37signals conventions of tapping variables in the > views: > > <% account.owner.tap do |user| %> > ... > <% end %> > > But, I find myself having to do this a lot... > > <% account.owner.tap do |user| %> > <% if user %> > ... > <% end %> > <% end %> > > It would be great if we could do... > > <% account.owner.tap_if(:present?) do |user| %> > ... > <% end %> > > <% account.users.tap_if(:any?) do |user| %> > ... > <% end %> > > The block would only yield if the method evals to true. > Carlos mentioned that you can add an "if account.owner.present?" at the > end... > But there are times when the account.owner (or something else) call is > expensive and you don''t want to call it twice. > > Any feedback would be much appreciated. Thanks! > > -- > 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 to > rubyonrails-core+unsubscribe@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- 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 to rubyonrails-core+unsubscribe@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
On Jan 25, 2013, at 5:02 PM, Andrew Kaspick wrote:> What''s wrong with... > <% if user = account.owner %> > ... > <% end %> > or > <% if users = account.users.presence %> > ... > <% end %> > ?If running with warnings enabled ruby will raise a warning on these. Best. Mike -- 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 to rubyonrails-core+unsubscribe@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Thank you Godfrey! This does solve 99% of my cases! @Andrew Using if, the variable "users" is now available everywhere...I only want it available in my block. On Friday, January 25, 2013 2:05:31 PM UTC-8, Godfrey Chan wrote:> > Object#try takes a block. It''ll probably take care of most simple cases > for you: > > <% account.owner.try do |user| %> > <!-- This is shown only if account.owner is not nil? --> > <% end %> > > > > On Fri, Jan 25, 2013 at 1:17 PM, Ngan <ngan...@gmail.com <javascript:>>wrote: > >> I originally brought this up in: >> https://github.com/rails/rails/issues/9067 >> >> Rails paved the way for Object#tap and Object#try...I''d like to propose >> Object#tap_if and its counterpart,Object#tap_unless. >> >> I''ve been following 37signals conventions of tapping variables in the >> views: >> >> <% account.owner.tap do |user| %> >> ... >> <% end %> >> >> But, I find myself having to do this a lot... >> >> <% account.owner.tap do |user| %> >> <% if user %> >> ... >> <% end %> >> <% end %> >> >> It would be great if we could do... >> >> <% account.owner.tap_if(:present?) do |user| %> >> ... >> <% end %> >> >> <% account.users.tap_if(:any?) do |user| %> >> ... >> <% end %> >> >> The block would only yield if the method evals to true. >> Carlos mentioned that you can add an "if account.owner.present?" at the >> end... >> But there are times when the account.owner (or something else) call is >> expensive and you don''t want to call it twice. >> >> Any feedback would be much appreciated. Thanks! >> >> -- >> 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 rubyonra...@googlegroups.com<javascript:> >> . >> To unsubscribe from this group, send email to >> rubyonrails-co...@googlegroups.com <javascript:>. >> Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en >> . >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > >-- 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 to rubyonrails-core+unsubscribe@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Actually, on second thought, this would do exactly what you wanted, and It''s Just Ruby (tm): <% account.users.any? && account.users.tap do |users| %> # ... <% end %> On Fri, Jan 25, 2013 at 2:09 PM, Ngan <nganpham@gmail.com> wrote:> Thank you Godfrey! This does solve 99% of my cases! > > @Andrew Using if, the variable "users" is now available everywhere...I > only want it available in my block. > > > On Friday, January 25, 2013 2:05:31 PM UTC-8, Godfrey Chan wrote: > >> Object#try takes a block. It''ll probably take care of most simple cases >> for you: >> >> <% account.owner.try do |user| %> >> <!-- This is shown only if account.owner is not nil? --> >> <% end %> >> >> >> >> On Fri, Jan 25, 2013 at 1:17 PM, Ngan <ngan...@gmail.com> wrote: >> >>> I originally brought this up in: https://github.com/rails/** >>> rails/issues/9067 <https://github.com/rails/rails/issues/9067> >>> >>> Rails paved the way for Object#tap and Object#try.**..I''d like to >>> propose Object#tap_if and its counterpart,Object#tap_unless. >>> >>> I''ve been following 37signals conventions of tapping variables in the >>> views: >>> >>> <% account.owner.tap do |user| %> >>> ... >>> <% end %> >>> >>> But, I find myself having to do this a lot... >>> >>> <% account.owner.tap do |user| %> >>> <% if user %> >>> ... >>> <% end %> >>> <% end %> >>> >>> It would be great if we could do... >>> >>> <% account.owner.tap_if(:present?**) do |user| %> >>> ... >>> <% end %> >>> >>> <% account.users.tap_if(:any?) do |user| %> >>> ... >>> <% end %> >>> >>> The block would only yield if the method evals to true. >>> Carlos mentioned that you can add an "if account.owner.present?" at the >>> end... >>> But there are times when the account.owner (or something else) call is >>> expensive and you don''t want to call it twice. >>> >>> Any feedback would be much appreciated. Thanks! >>> >>> -- >>> 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 rubyonra...@googlegroups.**com. >>> To unsubscribe from this group, send email to rubyonrails-co...@** >>> googlegroups.com. >>> >>> Visit this group at http://groups.google.com/** >>> group/rubyonrails-core?hl=en<http://groups.google.com/group/rubyonrails-core?hl=en> >>> . >>> For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> >>> . >>> >>> >>> >> >> -- > 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 to > rubyonrails-core+unsubscribe@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- 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 to rubyonrails-core+unsubscribe@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
The problem (I think) with that is that "#users" could be an expensive call you only wish to make once. And I don''t want to get into memoizing... <% account.expensive_call_for_users.any? && account.expensive_call_for_users.tap ... %> On Friday, January 25, 2013 2:11:18 PM UTC-8, Godfrey Chan wrote:> > Actually, on second thought, this would do exactly what you wanted, and > It''s Just Ruby (tm): > > <% account.users.any? && account.users.tap do |users| %> > > > # ... > <% end %> > > > > > On Fri, Jan 25, 2013 at 2:09 PM, Ngan <ngan...@gmail.com <javascript:>>wrote: > >> Thank you Godfrey! This does solve 99% of my cases! >> >> @Andrew Using if, the variable "users" is now available everywhere...I >> only want it available in my block. >> >> >> On Friday, January 25, 2013 2:05:31 PM UTC-8, Godfrey Chan wrote: >> >>> Object#try takes a block. It''ll probably take care of most simple cases >>> for you: >>> >>> <% account.owner.try do |user| %> >>> <!-- This is shown only if account.owner is not nil? --> >>> <% end %> >>> >>> >>> >>> On Fri, Jan 25, 2013 at 1:17 PM, Ngan <ngan...@gmail.com> wrote: >>> >>>> I originally brought this up in: https://github.com/rails/** >>>> rails/issues/9067 <https://github.com/rails/rails/issues/9067> >>>> >>>> Rails paved the way for Object#tap and Object#try.**..I''d like to >>>> propose Object#tap_if and its counterpart,Object#tap_unless. >>>> >>>> I''ve been following 37signals conventions of tapping variables in the >>>> views: >>>> >>>> <% account.owner.tap do |user| %> >>>> ... >>>> <% end %> >>>> >>>> But, I find myself having to do this a lot... >>>> >>>> <% account.owner.tap do |user| %> >>>> <% if user %> >>>> ... >>>> <% end %> >>>> <% end %> >>>> >>>> It would be great if we could do... >>>> >>>> <% account.owner.tap_if(:present?**) do |user| %> >>>> ... >>>> <% end %> >>>> >>>> <% account.users.tap_if(:any?) do |user| %> >>>> ... >>>> <% end %> >>>> >>>> The block would only yield if the method evals to true. >>>> Carlos mentioned that you can add an "if account.owner.present?" at the >>>> end... >>>> But there are times when the account.owner (or something else) call is >>>> expensive and you don''t want to call it twice. >>>> >>>> Any feedback would be much appreciated. Thanks! >>>> >>>> -- >>>> 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 rubyonra...@googlegroups.**com. >>>> To unsubscribe from this group, send email to rubyonrails-co...@** >>>> googlegroups.com. >>>> >>>> Visit this group at http://groups.google.com/** >>>> group/rubyonrails-core?hl=en<http://groups.google.com/group/rubyonrails-core?hl=en> >>>> . >>>> For more options, visit https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> >>>> . >>>> >>>> >>>> >>> >>> -- >> 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 rubyonra...@googlegroups.com<javascript:> >> . >> To unsubscribe from this group, send email to >> rubyonrails-co...@googlegroups.com <javascript:>. >> Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en >> . >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > >-- 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 to rubyonrails-core+unsubscribe@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
<% (users = account.users).any? && users.tap do |users| %> Less pretty, still works. -- Richard Schneeman http://heroku.com @schneems (http://twitter.com/schneems) On Friday, January 25, 2013 at 4:13 PM, Ngan wrote:> The problem (I think) with that is that "#users" could be an expensive call you only wish to make once. And I don''t want to get into memoizing... > > <% account.expensive_call_for_users.any? && account.expensive_call_for_users.tap ... %> > > > On Friday, January 25, 2013 2:11:18 PM UTC-8, Godfrey Chan wrote: > > Actually, on second thought, this would do exactly what you wanted, and It''s Just Ruby (tm): > > <% account.users.any? && account.users.tap do |users| %> > > # ... <% end %> > > > > > > > > On Fri, Jan 25, 2013 at 2:09 PM, Ngan <ngan...@gmail.com (javascript:)> wrote: > > > Thank you Godfrey! This does solve 99% of my cases! > > > > > > @Andrew Using if, the variable "users" is now available everywhere...I only want it available in my block. > > > > > > > > > On Friday, January 25, 2013 2:05:31 PM UTC-8, Godfrey Chan wrote: > > > > Object#try takes a block. It''ll probably take care of most simple cases for you: > > > > > > > > <% account.owner.try do |user| %> <!-- This is shown only if account.owner is not nil? --> <% end %> > > > > > > > > > > > > On Fri, Jan 25, 2013 at 1:17 PM, Ngan <ngan...@gmail.com> wrote: > > > > > I originally brought this up in: https://github.com/rails/rails/issues/9067 > > > > > > > > > > > > > > > Rails paved the way for Object#tap and Object#try...I''d like to propose Object#tap_if and its counterpart,Object#tap_unless. > > > > > > > > > > > > > > > I''ve been following 37signals conventions of tapping variables in the views: > > > > > > > > > > <% account.owner.tap do |user| %> ... <% end %> > > > > > > > > > > But, I find myself having to do this a lot... > > > > > > > > > > <% account.owner.tap do |user| %> <% if user %> ... <% end %> <% end %> > > > > > > > > > > It would be great if we could do... > > > > > > > > > > <% account.owner.tap_if(:present?) do |user| %> ... <% end %> <% account.users.tap_if(:any?) do |user| %> ... <% end %> > > > > > > > > > > The block would only yield if the method evals to true. > > > > > > > > > > > > > > > Carlos mentioned that you can add an "if account.owner.present?" at the end... > > > > > But there are times when the account.owner (or something else) call is expensive and you don''t want to call it twice. > > > > > > > > > > Any feedback would be much appreciated. Thanks! > > > > > > > > > > -- > > > > > 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 rubyonra...@googlegroups.com. > > > > > To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com. > > > > > > > > > > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > > > > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > > > > > > > > > > > > -- > > > 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 rubyonra...@googlegroups.com (javascript:). > > > To unsubscribe from this group, send email to rubyonrails-co...@googlegroups.com (javascript:). > > > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > > > > -- > 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 (mailto:rubyonrails-core@googlegroups.com). > To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com (mailto:rubyonrails-core+unsubscribe@googlegroups.com). > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 to rubyonrails-core+unsubscribe@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Why not just break unless user inside the block? Not sure if it should be break, next, or return in that context but you get the idea. I always prefer earlier returns for such case. Em 25/01/2013 19:17, "Ngan" <nganpham@gmail.com> escreveu:> I originally brought this up in: > https://github.com/rails/rails/issues/9067 > > Rails paved the way for Object#tap and Object#try...I''d like to propose > Object#tap_if and its counterpart,Object#tap_unless. > > I''ve been following 37signals conventions of tapping variables in the > views: > > <% account.owner.tap do |user| %> > ... > <% end %> > > But, I find myself having to do this a lot... > > <% account.owner.tap do |user| %> > <% if user %> > ... > <% end %> > <% end %> > > It would be great if we could do... > > <% account.owner.tap_if(:present?) do |user| %> > ... > <% end %> > > <% account.users.tap_if(:any?) do |user| %> > ... > <% end %> > > The block would only yield if the method evals to true. > Carlos mentioned that you can add an "if account.owner.present?" at the > end... > But there are times when the account.owner (or something else) call is > expensive and you don''t want to call it twice. > > Any feedback would be much appreciated. Thanks! > > -- > 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 to > rubyonrails-core+unsubscribe@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > >-- 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 to rubyonrails-core+unsubscribe@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Not to advocate one may or the other but github.com/bnorton/tap_if On Friday, January 25, 2013 1:17:49 PM UTC-8, Ngan wrote:> I originally brought this up in: https://github.com/rails/rails/issues/9067 > > > > Rails paved the way for Object#tap and Object#try...I''d like to propose Object#tap_if and its counterpart,Object#tap_unless. > I''ve been following 37signals conventions of tapping variables in the views:<% account.owner.tap do |user| %> > ... > <% end %> > > But, I find myself having to do this a lot...<% account.owner.tap do |user| %> > <% if user %> > ... > <% end %> > <% end %> > > It would be great if we could do...<% account.owner.tap_if(:present?) do |user| %> > ... > <% end %> > > <% account.users.tap_if(:any?) do |user| %> > ... > <% end %> > > The block would only yield if the method evals to true. > Carlos mentioned that you can add an "if account.owner.present?" at the end... > > But there are times when the account.owner (or something else) call is expensive and you don''t want to call it twice. > > > Any feedback would be much appreciated. Thanks!-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscribe@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core?hl=en. For more options, visit https://groups.google.com/groups/opt_out.