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
Post a Comment