Monday, February 19, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...