Ruby on Rails Thursday, April 26, 2018

> On Apr 26, 2018, at 12:12 PM, Abdel Latif <mlotfi2005@gmail.com> wrote:
>
> Hi,
>
> I found this code for sorting an array :
>
> p ["AB_1020", "AB_950", "AB_50", "1000", "570"].partition{|x| x.to_i.zero? }
> .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}
> ouptut
>
> #⇒ ["AB_50", "AB_950", "AB_1020", "570", "1000"]
>
>
> I know that .partition{|x| x.to_i.zero? } will create an array of arrays :
> [["AB_1020", "AB_950", "AB_50"], ["1000", "570"]]
>
>
> But this part .flat_map{|x| x.sort_by {|x|x[/d+/]}.reverse}

flat_map: https://apidock.com/ruby/Enumerable/flat_map

Then that passes a block, which acts on each member of the Enumerable that called flat_map. That block sorts by the numerical value of x, then reverses.

Does that help?

Walter

>
> I could understand it.
>
> Thanks.
>
> --
> 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@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c8e2dc7d-8501-4df3-b19c-917ebded457f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/F80F3086-EFC6-4E2F-99E8-07C60AAD479D%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment