Ram
2010-Jul-16 12:48 UTC
Ruby - return true if any element in Array satisfies condition but false otherwise
Hi, A Ruby question. Anyone know a function that loops through an array and returns true if any one element satisfies the condition but false if none satisfy the condition? And it should do this in one line. Something like any_bot_follower = followers.each {|f| return true if f.bot?} The above returns an array if none of the Array elements satisfy the condition. I want it to return false instead. I have seen a function somewhere that does this but just not able to nail it down. Hoping one of you here would know. Thanks! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-Jul-16 12:51 UTC
Re: Ruby - return true if any element in Array satisfies condition but false otherwise
On 16 July 2010 13:48, Ram <yourstruly.vinay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Anyone know a function that loops through an array and returns true if > any one element satisfies the condition but false if none satisfy the > condition? And it should do this in one line. Something likeYou probably want ".detect" http://ruby-doc.org/core/classes/Enumerable.html#M003123 any_bot_follower = followers.detect(false) { |f| f.bot? } -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Andy Jeffries
2010-Jul-16 13:19 UTC
Re: Ruby - return true if any element in Array satisfies condition but false otherwise
On 16 July 2010 13:51, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 16 July 2010 13:48, Ram <yourstruly.vinay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Anyone know a function that loops through an array and returns true if > > any one element satisfies the condition but false if none satisfy the > > condition? And it should do this in one line. Something like > > You probably want ".detect" > http://ruby-doc.org/core/classes/Enumerable.html#M003123 > > any_bot_follower = followers.detect(false) { |f| f.bot? } >Or Enumerable#any? http://apidock.com/ruby/Enumerable/any%3F any_bot_follower = followers.any? { |f| f.bot? } Cheers, Andy -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-Jul-16 13:42 UTC
Re: Ruby - return true if any element in Array satisfies condition but false otherwise
On 16 July 2010 14:19, Andy Jeffries <andy-4Fjv1yF9AWJvmwGHilWZ5bVCufUGDwFn@public.gmane.org> wrote:> On 16 July 2010 13:51, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> You probably want ".detect" > > Or Enumerable#any? > http://apidock.com/ruby/Enumerable/any%3F > any_bot_follower = followers.any? { |f| f.bot? }Even better! As it just returns true/false (what the OP asked for) Goes to show what reading the manual can give you :-D -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jul-16 14:07 UTC
Re: Ruby - return true if any element in Array satisfies condition but false otherwise
On Jul 16, 1:48 pm, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > A Ruby question. > > Anyone know a function that loops through an array and returns true if > any one element satisfies the condition but false if none satisfy the > condition? And it should do this in one line. Something like >any? Fred> any_bot_follower = followers.each {|f| return true if f.bot?} > > The above returns an array if none of the Array elements satisfy the > condition. I want it to return false instead. > > I have seen a function somewhere that does this but just not able to > nail it down. Hoping one of you here would know. > > Thanks!-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ram
2010-Jul-19 06:13 UTC
Re: Ruby - return true if any element in Array satisfies condition but false otherwise
Perfect! :) thanks! Couldn''t find docs on this though. On Jul 16, 7:07 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jul 16, 1:48 pm, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > ARubyquestion. > > > Anyone know a function that loops through anarrayand returns true if > > any oneelementsatisfies theconditionbut false if none satisfy the > >condition? And it should do this in one line. Something like > > any? > > Fred > > > > > any_bot_follower = followers.each {|f| return true if f.bot?} > > > The above returns anarrayif none of theArrayelements satisfy the > >condition. I want it to return false instead. > > > I have seen a function somewhere that does this but just not able to > > nail it down. Hoping one of you here would know. > > > Thanks!-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
pepe
2010-Jul-19 08:44 UTC
Re: Ruby - return true if any element in Array satisfies condition but false otherwise
Array includes module Enumerable. ''any?'' can be found in Enumerable. On Jul 19, 2:13 am, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Perfect! :) thanks! Couldn''t find docs on this though. > > On Jul 16, 7:07 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Jul 16, 1:48 pm, Ram <yourstruly.vi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hi, > > > > ARubyquestion. > > > > Anyone know a function that loops through anarrayand returns true if > > > any oneelementsatisfies theconditionbut false if none satisfy the > > >condition? And it should do this in one line. Something like > > > any? > > > Fred > > > > any_bot_follower = followers.each {|f| return true if f.bot?} > > > > The above returns anarrayif none of theArrayelements satisfy the > > >condition. I want it to return false instead. > > > > I have seen a function somewhere that does this but just not able to > > > nail it down. Hoping one of you here would know. > > > > Thanks!-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.