Dropping elements left and right: fun with ruby arrays
Let’s say we have an array of hashes, that represents a time series: DATA_POINTS = [ { date: ‘2016-01-01’, value: nil }, { date: ‘2016-02-01’, value: nil }, { date: ‘2016-03-01’, value: 100 }, { date: ‘2016-04-01’, value: nil }, { date: ‘2016-05-01’, value: 300 }, { date: ‘2016-06-01’, value: nil }, { date: ‘2016-07-01’, value: nil } ] What we want to do with it is to remove all elements with a nil value from the array’s beginning and end, similar to what String#strip does for strings. Ruby arrays have a bunch of neat methods, one of them is Array#drop_while, it drops elements from the beginning of an array until the passed in block evaluates to false or nil. Let’s try it on our array: DATA_POINTS.drop_while {|element| element[:value].nil?} =>…