Monday, 19 February 2018

ruby - How do I strip off nil elements from the end of an array if all my elements are nil?



I'm using Ruby 2.4. I want to strip off nil elements from the end of an array, so I'm using



row_data.pop until row_data.last


but if the array only contains nil elements, this seems to cause some kind of infinite loop because the call never returns. How do I account for the fact that the array might contain all nil elements?


Answer



Just add an extra check to see if the array is empty.




row_data.pop until row_data.last || row_data.empty?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...