Ruby Count Array objects if object includes value

来源:互联网 发布:厂家销售软件 编辑:程序博客网 时间:2024/05/21 07:58

I have an array:

array = ['Footballs','Baseball','football','Soccer']

and I need to count the number of times Football or Baseball is seen, regardless of case and pluralization.


array.count { |element| element.match(/(football|baseball)s?\Z/i) }

This will match any of these elements: football, footballs, baseball, baseballs.

The s? makes the 's' optional, the i option (//i) makes the expression case insensitive, and the\Z option checks for the end of the string.


0 0