Ruby Cycle and Looping
Ruby:Enumberable#Cycle
The #cycle method uses a block for each element of an enumerable object (ranges, hash, or array) repeatedly for n times or endlessly if n isn't specified. If a negative number or the source is empty then cycle returns nothing. It also returns nil if the loop finishes without getting interrupted. Cycle saves elements to an internal array so changes to enum after the first pass no effect; it is a nondestructive method.
In the example above we call ".cycle" on array. It will then iterate over each element in array for the length of the array to finish one cycle. We specify 2 cycles so it will iterate over the array a second time. Without specifying n the code will run numerous cycles. With this in mind we should also note #cycle does not specify the number of element to cycle through. Additionally, if #cycle is not passed a block it will return an Enumerator.
Looping Methods
The While loop
This loop executes code while a condition is TRUE. The conditional can be separated by "do", a new line, backslash, or semicolon. Use the while loop when you want code to run while a condition is true. the term "break" may be used in a while loop if you want the loop to stop executing code before the condition becomes false.
The Until Loop executes code while the conditional is False. Please take note this is different from the while loop. Similar to the while loop, the code is separated by "do", a new line, or a semicolon. This loop is in the same format as the while loop. Just replace while with until. Use the while loop when you want code to run until the condition becomes false.
The For statement executes code once for each element in the expression. A For loop is the same as " (expression).each do |variable| code end" except that the For loop doesn't create a new scope for local variables. A for loop expression is separated from code by..guess? "do", a new line, or a semicolon. Use a for loop when you want code to run a specific number of times.
Posts
-
The Post DBC Experience
Mixed Emotions
DBC was an awesome, maybe one of a kind experience. I pushed through all 4 phases with the mantids, or the improper plural...
-
How to Ask for Help
How to Ask Good Questions
Experienced Questioning
I don't always ask questions online but when I do I try to be as specific as possible...
-
Ruby on Rails
Rails
Ruby and Rails are not the same thing. Ruby is the programming language. Rails is a Rubygem or package library which is installed via...
-
Resolving Conflicts
A Conflict I Experienced
Honestly, I cannot remember a conflict I've experienced off the top of my head that would be exciting to read or...
- My Thinking Ways How do I Think? I am a concrete sequential thinker which according to cgribben.com mean that I am persistent and product oriented. I am practical...
-
SQL Injection
What is SQL?
SQL is short for Structured Query Language. It is a programming language for querying (fancy word for accessing) and managing databases. SQL...
-
Affirmation and Stereotype Threat
My Values
Accomplishment, accountability, compassion, creativity, efficiency, family, friendships, and integrity area few values which come to mind when I recall the times in my...
-
Blocs, Procs, and Lambdas
Ruby Blocks, Procs, Lambdas
Blocks
A ruby block is the piece of code that is executed. This code can be executed using curly brackets {}...
-
Stereotype Threat
What is Stereotype Threat?
To put it simply, stereotype threat is the experience of not living or carrying out your full potential because of stereotypes....
-
A Pair of Minds
Pairing and Feedback
Pairing
Pairing isn't so bad. The peers are all nice and I haven't met someone who was disrespectful during the sessions. Despite...
-
A Little Ruby
A Snippet into Ruby Classes
When and Why Should I use classes?
One question which kept simmering in my head while working on this week's...
-
Ruby Cycle and Looping
Ruby:Enumberable#Cycle
The #cycle method uses a block for each element of an enumerable object (ranges, hash, or array) repeatedly for n times or endlessly if...
-
Today's Software Apps vs Law
Who Doesn’t Like App Efficiency?
Innovative technologies usually clash with old ways and methods. There are numerous apps out there which are constantly clashing with...
-
What is JS?
What is JavaScript and Why Should You Care?
Javascript(JS) is an object oriented computer programming language often found as a part of web browsers which...
-
CSS Positioning
Positioning in CSS
Elements on a website can be positioned behind, in front of, or next to by using CSS positioning properties: top, left, right,...
-
My Favorite Websites
My 3 Most Visited Websites
Newcelica.org
This is one of my favorite sites because I own a Celica. I do all...
-
Kitchen Vs Table
Kitchen Vs Table
DBC sounds like a buttkicker, a mental, emotional, and physical obstacle course. Honestly, it sounds like it would and should be overwhelming...