Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Sharpen your Ruby: Part 5
Eric The Coder
Eric The Coder

Posted on • Edited on • Originally published ateric-the-coder.com

     

Sharpen your Ruby: Part 5

I develop in Javascript, Python, PHP, and Ruby. By far Ruby is my favorite programming language.

Together let start a journey and revisit our Ruby foundations.

Follow me on Twitter:EricTheCoder_

Classic Loop

In Ruby like any other programming language we can iterate a number of fix or variable times. Here are a classic infinite loop that will execute until the program crash.

loopdoput'Hello World'end
Enter fullscreen modeExit fullscreen mode

While loop

It is possible loop while a condition is meet.

number=0whilenumber<100putsnumbernumber+=1end# Will print numbers from 0 to 99
Enter fullscreen modeExit fullscreen mode

Loop until

It is possible loop until a condition is meet.

number=0untilnumber==10putsnumbernumber+=1end# Will print numbers from 0 to 9
Enter fullscreen modeExit fullscreen mode

Loop x number of times

(1..10).eachdo|i|putsiend# print numbers from 1 to 19
Enter fullscreen modeExit fullscreen mode

or also

10.times{puts"Hello World"}
Enter fullscreen modeExit fullscreen mode

Loop through each element

By far, the most use loop is the 'each' iteration

# Array of namesnames=['Peter','Mike','John']# Iterate the names listnames.eachdo|name|putsnameend# or shorthand versionnames.each{|name|putsname}
Enter fullscreen modeExit fullscreen mode

Break and Continue

It is possible to stop the loop before the end. It is also possible to skip one iteration and go to the next one.

names.eachdo|name|nextifname==='Mike'putsnameend# Peter# John
Enter fullscreen modeExit fullscreen mode

The iteration will skip the puts statement if the name is equal to Mike

names.eachdo|name|breakifname==='Mike'putsnameend# Peter
Enter fullscreen modeExit fullscreen mode

The iteration will stop if the name is equal to Mike.

Exercise

Create a little program that:

  • Create a loop with 10 iterations (from number 1 to 10)
  • Each iteration will print only if the iteration number is even. Odd number will be skip.

Solution

10.timesdo|num|nextifnum.odd?puts"Number#{num}"end
Enter fullscreen modeExit fullscreen mode

Conclusion

That's it for today. The journey just started, stay tune for the next post very soon. (later today or tomorrow)

If you have any comments or questions please do so here or send me a message on twitter.

Follow me on Twitter:EricTheCoder_

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
dhavalsingh profile image
Dhaval Singh
RoR dev
  • Education
    Bits Pilani Goa Campus
  • Work
    Anarock
  • Joined

in the -> Loop x number of times part it should be
# print numbers from 1 to 10.
Good article for new learners btw!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Businessman and blogger #Javascript, #Python and #PHP. My favorite frameworks/librairies are #React, #Laravel, and #Django. I am also a fan of #TailwindCSS
  • Location
    Canada
  • Joined

More fromEric The Coder

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp