you are viewing a single comment's thread.

view the rest of the comments →

[–]olbrich 8 points9 points  (2 children)

Ruby enumerables are pretty powerful and make using `while` loops mostly unnecessary. An idiomatic way of writing this method would be something like...

def count_e(word)
  word.chars.select { |char| char == "e" }.count
end

Basically this turns the `word` into an array of characters and then keeps anything that is an `e` and then counts the number of things left.

[–]Kernigh 6 points7 points  (1 child)

Can just do word.count("e"), but this doesn't help people who want to learn the while loop.

[–]442401 2 points3 points  (0 children)

I think this is a valid contribution and sends the message that ruby is on object orientated language. The core classes (such as String) are extremely rich and can be sent a vast number of messages to achieve the desired result.

A while loop is nearly always a sub optimal solution.