How to select the last n % elements of an array in Ruby? -


i have sorted array , keep last 50-60-70% (generally n%) of elements. how can achieve this?

for example

[1,2,3,4,5,6,7,8,9].last(this.size*0.7)  

doesn't work.

you need use tap in order reference object want.

[1,2,3,4,5,6,7,8,9].tap { |this| break this.last(this.size*0.7) } 

the break needed because tap ignores block's return value.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -