arrays in Ruby, how to handle this situation? -
let's have following array:
arr = ["", "2121", "8", "mystring"]
i want return false in case array contains non-digit symbols.
if empty strings allowed:
def contains_non_digit(array) !array.select {|s| s =~ /^.*[^0-9].*$/}.empty? end
explanation: filters array strings match regular expression. regex true string contains @ least 1 non-digit character. if resulting array empty, array contains no non-digit strings. finally, need negate result, because want know array does contain non-digit strings.
Comments
Post a Comment