0

I want to count how much D and E and F contains the array ary in total.

I can do it like

ary.count('D') + ary.count('E') + ary.count('F')

or like

count = 0
'DEF'.split('').each do |letter|
  count += ary.count(letter)
end
count

but both of it don't look very smart to me, is there a better way in ruby? Unfortunately, .count('D','E','F') does not work.

5
  • Is ary an array of 1-character strings? If not, and one element of ary is "DDr", does that count as 0, 1 or 2 'D''s? Commented Oct 31, 2017 at 19:02
  • @CarySwoveland: well, if provided examples are any indication, then it's an array of one-char strings. Commented Oct 31, 2017 at 20:47
  • Ready to select an answer? Commented Nov 6, 2017 at 13:56
  • the problem is, my question somehow violated the stackoverflow question restrictions, thats why I cant accept an answer now because there is no real answer :) Commented Nov 6, 2017 at 17:43
  • This question is not on hold or otherwise closed or locked. I see no reason why you can't click the green checkmark by my answer (or one of the others :) ) Commented Nov 8, 2017 at 15:00

4 Answers 4

3

You could use a regex.

ary.grep(/\A(D|E|F)\z/).size

You can easily build this regex from array ['D', 'E', 'F'], but I'll leave that up to you.

Sign up to request clarification or add additional context in comments.

3 Comments

Why the anchors? I must be missing something obvious.
@CarySwoveland To match single-letter strings and not, say, strings like "F grade".
See my comment on the question.
3

It's pretty straightforward if you're simply counting letters:

letters = 'AEIOU'.chars

matches = 'FISSION'.chars.grep(Regexp.union(letters)).count

Where this is useful for matching single letter instances in a case-sensitive manner. Here Regexp.union creates a single regular expression that matches any of those letters.

5 Comments

that does not look simpler to me
A tiny suggestion: 'FISSION.each_char.grep....
@CarySwoveland Why each_char vs. chars?
To avoid creating a temporary array. I'm in the habit of using each_char whenever an Enumerable follows.
@CarySwoveland Ah, that is a good point. For repeat operations that'd be more efficient.
1

You can pass a block to Array#count, as follows:

ary.count { |item| %w(D E F).include?(item) }

This will return a count of how many elements for which the block returns a truthy (not nil or false) value.

%w() is a nice syntax for defining an array of strings - i.e. the following are equivalent:

['D', 'E', 'F'] == %w(D E F)

3 Comments

Maybe just 'DEF'.include?(item).
Maybe, but this will also match "DE", "EF" and "DEF". If a strict single letter match is needed, this won't suffice
We need clarification. I left a comment on the question.
0

You were close. It's actually very simple, using a little-known feature of String#count:

'DEF'.count('DEF') #=> 3
'FEED ME'.count('DEF') #=> 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.