Cool, I wasn't actually using that code, just threw it together to illustrate
what I was after. #try does what I want, so that's handy.
On Thursday, 14 June 2012 at 1:16 PM, Ben Hoskings wrote:
> Also it's worth mentioning that code sample of yours won't do what you expect
> -- tap always returns its caller, but your method will return the block value
> for non-nil receivers.
>
> The logic should be:
>
> class Object
> def tap_unless_nil
> yield(self) unless nil?
> self
> end
> end
>
> Again, wouldn't endorse it, I'm just talkin' hypotheticals here :)
>
> - Ben
>
>
>
> On 14/06/2012, at 11:01 AM, Steve H wrote:
>
> > I'm looking for something built-in to Ruby that works kind of like
> > Object#tap, but only yields if self is not nil.
> >
> > That is, can I do this without monkey-patching Object?
> >
> > If it's in Activesupport that's fine too. I feel like this method exists
> > but I can't find it.
> >
> > class Object
> > def tap_unless_nil
> > if nil?
> > nil
> > else
> > yield self
> > end
> > end
> > end
> >
> > "hello".tap_unless_nil{ |a| puts "yielded #{a.inspect}" }
> > nil.tap_unless_nil{ |a| puts "yielded #{a.inspect}" }
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Ruby or Rails Oceania" group.
> > To view this discussion on the web visit
> > https://groups.google.com/d/msg/rails-oceania/-/XkiiF0QABO0J.
> > To post to this group, send email to [email protected]
> > (mailto:[email protected]).
> > To unsubscribe from this group, send email to
> > [email protected]
> > (mailto:[email protected]).
> > For more options, visit this group at
> > http://groups.google.com/group/rails-oceania?hl=en.
> >
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected]
> (mailto:[email protected]).
> To unsubscribe from this group, send email to
> [email protected]
> (mailto:[email protected]).
> For more options, visit this group at
> http://groups.google.com/group/rails-oceania?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
or Rails Oceania" 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/rails-oceania?hl=en.