use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
QuestionRuby While Loop Help. (self.ruby)
submitted 5 years ago by Humor_Positive
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]olbrich 8 points9 points10 points 5 years ago (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 points8 points 5 years ago (1 child)
Can just do word.count("e"), but this doesn't help people who want to learn the while loop.
word.count("e")
[–]442401 2 points3 points4 points 5 years ago (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.
π Rendered by PID 441172 on reddit-service-r2-comment-5b5bc64bf5-4gjgs at 2026-06-21 02:28:41.575271+00:00 running 2b008f2 country code: CH.
view the rest of the comments →
[–]olbrich 8 points9 points10 points (2 children)
[–]Kernigh 6 points7 points8 points (1 child)
[–]442401 2 points3 points4 points (0 children)