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

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -