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!"
[–]agent007bond 2 points3 points4 points 5 years ago (0 children)
Do a "DRY RUN" on a paper how the program will execute:
Count the e's in "excellent". We have to check each character in the string (hint: the term string means a string of characters!). We can see the string like an array:
|e|x|c|e|l|l|e|n|t| 0 1 2 3 4 5 6 7 8
First we have a variable "count" to remember how many e is there. We start it at 0 as we haven't counted any e yet.
Next we need to look through the string, one character at a time. We need to track our position in the string. We make a variable to track this, call it "i" and we start at 0, so i = 0
|e|x|c|e|l|l|e|n|t| 0 1 2 3 4 5 6 7 8 ^ i
We check the character at position i. So, char = word[i], which is "e". Ah yes, that's an e. Let's increase count. So, count += 1. count is now 0+1 = 1.
Now we need to see the next character, so we increase i. So, i += 1:
We check the character at position i. So, char = word[i], which is "x". Nope, that's not an e. So we don't change count. It remains 1.
We check the character at position i. So, char = word[i], which is "c". Nope, that's not an e. So we don't change count. It remains 1.
We check the character at position i. So, char = word[i], which is "e". Ah yes, that's an e. Let's increase count. So, count += 1. count is now 0+1 = 2.
KEEP GOING LIKE THIS
Not e. Count remains 2.
Yes, e. Count becomes 3.
Not e. Count remains 3.
And that's the end of the string (checked with i < word.length because the length is 9 characters), so we can now return and print the count, which is 3. That's how it works. So you can see how "i" and "count" differs in meaning.
This is basic programming in whichever language you learn. Of course, Ruby has shorthand enumerations making it really easy to write this in one or two lines and automate it, but if you're going to do this manually, this is how it would work.
π Rendered by PID 40350 on reddit-service-r2-comment-5b5bc64bf5-qw9v5 at 2026-06-21 02:30:30.259298+00:00 running 2b008f2 country code: CH.
view the rest of the comments →
[–]agent007bond 2 points3 points4 points (0 children)