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

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -