ruby - Delete leading nils from Array -
i have array so:
[nil, nil, nil, 2, 4, 6, 1, nil, nil, 3, 4, 6]
what need nice way remove leading nils in place (so compact!
bad), , number of nils removed (so drop_while
bad).
probably not idea, if insist on one-liner:
arr.instance_eval { shift(index { |element| !element.nil? } || size) }.count
edit: anyone's future reference, better looking version agreed upon:
arr.shift(arr.index(&:itself) || arr.size).count
Comments
Post a Comment