I wonder why ruby core method ''take_while'' does not work as expected in the below Rails console example: irb(main):003:0> arr = [] => [] irb(main):004:0> arr << Date.today - 15.days => [Sun, 27 Jan 2013] irb(main):005:0> arr << Date.today - 25.days => [Sun, 27 Jan 2013, Thu, 17 Jan 2013] irb(main):006:0> arr << Date.today => [Sun, 27 Jan 2013, Thu, 17 Jan 2013, Mon, 11 Feb 2013] irb(main):007:0> arr << Date.today + 10.days => [Sun, 27 Jan 2013, Thu, 17 Jan 2013, Mon, 11 Feb 2013, Thu, 21 Feb 2013] irb(main):008:0> arr << Date.today + 25.days => [Sun, 27 Jan 2013, Thu, 17 Jan 2013, Mon, 11 Feb 2013, Thu, 21 Feb 2013, Fri, 08 Mar 2013] irb(main):009:0> arr << Date.today + 35.days => [Sun, 27 Jan 2013, Thu, 17 Jan 2013, Mon, 11 Feb 2013, Thu, 21 Feb 2013, Fri, 08 Mar 2013, Mon, 18 Mar 2013] irb(main):010:0> arr.size => 6 irb(main):011:0> jan = arr.take_while {|d| d.month == 1 } => [Sun, 27 Jan 2013, Thu, 17 Jan 2013] irb(main):012:0> feb = arr.take_while {|d| d.month == 2 } => [] So, as you see, the first array that should contain January date only worked fine, but not the second one, or February dates; Any idea? Thanks and regards -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/CiibzXMDVesJ. For more options, visit https://groups.google.com/groups/opt_out.
Jordon Bedwell
2013-Feb-11 14:46 UTC
Re: split dates array with take_while does not work ?
On Mon, Feb 11, 2013 at 8:33 AM, Javix <s.cambour-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I wonder why ruby core method ''take_while'' does not work as expected in the > below Rails console example: > > irb(main):003:0> arr = [] > => []Enumerable#take_while short-circuits on nil or false, hence the while in it''s name. Use Enumerable#select -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Monday, February 11, 2013 3:46:46 PM UTC+1, Jordon Bedwell wrote:> > On Mon, Feb 11, 2013 at 8:33 AM, Javix <s.ca...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <javascript:>> > wrote: > > I wonder why ruby core method ''take_while'' does not work as expected in > the > > below Rails console example: > > > > irb(main):003:0> arr = [] > > => [] > > Yep, exact, ''select'' works as needed. By the way, is there a way (I findno method neither in Rails API for Array nor in Ruby core library, to split an array in sub-arrays by month ? Thanks> Enumerable#take_while short-circuits on nil or false, hence the while > in it''s name. Use Enumerable#select >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/fma6xIvJjUAJ. For more options, visit https://groups.google.com/groups/opt_out.
Jordon Bedwell
2013-Feb-11 15:20 UTC
Re: split dates array with take_while does not work ?
On Mon, Feb 11, 2013 at 9:15 AM, Javix <s.cambour-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Monday, February 11, 2013 3:46:46 PM UTC+1, Jordon Bedwell wrote: >> >> On Mon, Feb 11, 2013 at 8:33 AM, Javix <s.ca...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > I wonder why ruby core method ''take_while'' does not work as expected in >> > the >> > below Rails console example: >> > >> > irb(main):003:0> arr = [] >> > => [] >> > Yep, exact, ''select'' works as needed. By the way, is there a way (I find no > method neither in Rails API for Array nor in Ruby core library, to split an > array in sub-arrays by month ?date_array.chunk { |v| v.month }.map { |v| v.last } -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Monday, February 11, 2013 4:20:56 PM UTC+1, Jordon Bedwell wrote:> > On Mon, Feb 11, 2013 at 9:15 AM, Javix <s.ca...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <javascript:>> > wrote: > > > > > > On Monday, February 11, 2013 3:46:46 PM UTC+1, Jordon Bedwell wrote: > >> > >> On Mon, Feb 11, 2013 at 8:33 AM, Javix <s.ca...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> > I wonder why ruby core method ''take_while'' does not work as expected > in > >> > the > >> > below Rails console example: > >> > > >> > irb(main):003:0> arr = [] > >> > => [] > >> > > Yep, exact, ''select'' works as needed. By the way, is there a way (I find > no > > method neither in Rails API for Array nor in Ruby core library, to split > an > > array in sub-arrays by month ? > > Thank ou again, it worked. I fiund another way to do that with group_bymethod: arr.group_by(&:month) the only difference will be that the result will be a hash and not an array. Regards> date_array.chunk { |v| v.month }.map { |v| v.last } >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/dEiI_6zEq4IJ. For more options, visit https://groups.google.com/groups/opt_out.
Jordon Bedwell
2013-Feb-11 15:25 UTC
Re: split dates array with take_while does not work ?
On Mon, Feb 11, 2013 at 9:20 AM, Jordon Bedwell <envygeeks-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:>> Yep, exact, ''select'' works as needed. By the way, is there a way (I find no >> method neither in Rails API for Array nor in Ruby core library, to split an >> array in sub-arrays by month ? > > date_array.chunk { |v| v.month }.map { |v| v.last }You can also use group_by to turn it into a hash with the month as the key. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.