how to eliminate empty arrays from an array of arrays in ruby? -


i have array of arrays. want eliminate empty arrays.

iam using reject!(&:empty?) method. giving me unexpected results.

2.2.2 :306 >   array = ["yes","yes","yes"]  => ["yes", "yes", "yes"]  2.2.2 :307 > array.split("no")  => [["yes", "yes", "yes"]]  2.2.2 :308 > array.split("no").reject!(&:empty?)  => nil  2.2.2 :309 > array_with_no = ["yes","yes","yes","no"]  => ["yes", "yes", "yes", "no"]  2.2.2 :310 > array_with_no.split("no")  => [["yes", "yes", "yes"], []]  2.2.2 :311 > array.split("no").reject!(&:empty?)  => nil  2.2.2 :312 > array_with_no.split("no").reject!(&:empty?)  => [["yes", "yes", "yes"]]  2.2.2 :313 >  

i want result when there no empty array eliminate, should return same array instead of returning nil

you want reject instead of reject! (the !-version modifies array in-place , returns true or nil depending on if changes array made or not)

documentation on methods do


Comments

Popular posts from this blog

wireshark - USB mapping with python -

c++ - nodejs socket.io closes connection before upgrading to websocket -

Deploying Qt Application on Android is really slow? -