Ruby Programming – 14 – All about Loops

Ruby programming tutorial on loops. Ruby lesson.
★☆★ THE BEST EDITOR and IDE FOR PROGRAMMING ► http://bit.ly/Komodo-IDE-Learn-Ruby ★☆★

★☆★ SIGNUP TO GET MY RUBY BOOK FOR FREE ★☆★

http://bit.ly/free-book-signup

★☆★ FOLLOW ME BELOW ★☆★

Twitter ► http://bit.ly/jake-day-twitter

YouTube ► http://bit.ly/jake-day-youtube

Facebook ► http://bit.ly/jake-day-facebook

SoundCloud ► http://bit.ly/jake-day-soundcloud

★☆★ NOTES ★☆★

Topics discussed:
loop do, while, until, next if, break if

while
until
for variable in 1..30
loop do
-=
+=
=! not equal to
break
break if
next if

Comments

David Hill says:

Hey Jake, thanks very much for this Ruby series. It’s a really nice complement to books and online tutorials like Codecademy. Sometimes the tutorials can be really frustrating because the hint and Q&A board might not be enough to get you out of being stuck on a problem, killing your momentum and motivation.

As an extra means of practice, it would be really cool if your videos were accompanied by a description of exactly what will be accomplished in that lesson (i.e, written like a homework problem, just like the ‘Review’ exercises of the Codecademy lessons). That way, learners could try out the problem first if they think they might be able to do it on their own, and then use the video to check against their work or for help if they can’t make it run. 

Unbroken DBZ says:

Hey Jake, why do i get the report “No such file or directory” …(LoadError)?
I have typed the same like you.

Andrew Gillett says:

4:26 “so what that does is it makes bomb_timer negative one” – no, it is shorthand for subtracting one from bomb_timer

papa bur says:

You didnt make a 13?

Chris Son says:

Hi, I was wondering if there is any difference in result between your code:

loop do
any_number -= 5
next if any_number % 2 != 0
puts “#{any_number}”
break if any_number <= 0 end and this: loop do any_number -= 5 puts “#{any_number}” if any_number % 2 == 0 break if any_number <= 0 end Thanks.

Yvigny Goncharov says:

Why
.    bomb_timer = bomb_timer – 1
instead of
.    bomb_timer -= 1
???
You already used that construct in an  earlier lesson, so why use the more verbose one here, and explain it all over again?

Yvigny Goncharov says:

While I would not be anywhere near as rude as Jędrek, I do concur that you should have long since explained the use of the up arrow, which is actually pretty common on command-line systems, and switched to using that, probably taking the time to explain it for two to three videos and presuming people knew what was happening after that point, by simply saying “… and up arrow to recall the last command”. The drag and drop stuff is really, really, **really** tedious by this point…

The Walking Shadow says:

Thx to the loops i coded my own loggin program… (with a calculator ;P)

Marcelo Sakai says:

(error)
                   Time_fix = 50
                    until Time_fix < = 20                     puts Time_fix [line5]             time_fix = time_fix - 1 end (then, in the prompt) --> :5:in ‘

‘: undefined method “-” for nil:NilClass

what that means?

Joel Miller says:

I’m using Ruby 2.1.5.  Replicating your breaker.rb program; if I used < = it would return one minus integer after 0.  If I used >= it would only output the first integer.  I could get >= to go to 0; but it then would revert back to only outputting the first integer upon repetition.  Is this a bug in Ruby or me?

Wendy Campbell says:

You are such a great teacher! Thanks for explaining everything so simply.

Corncob Jenkins says:

yo dont put bomb timer or use that next time you make a video its mad weird listening to this video in public

Sara Abd Elfattah says:

I can ‘t understand the last example of loop do & next if :(

Mark Bouwman says:

i capitalized the “t” in time_fix :=> (Time_fix) 
it would print out a number and then a line stating the directory the file was in ending with

:5: warning: already initialized constant Time_fix

what does this mean exactly?

Wendy Campbell says:

Why did you put puts “#{even}” using the speech marks-curly braces to print the next if loop, would puts even not have worked?

Jędrek Domański says:

Did you know that pressing UP arrow on the keyboard invokes previousy used command in the ocmmand prompt? I watched 14 videos and got sick of you dragging and dropping ruby files into the command prompt. Noob!

Mendy Friedman says:

These videos are extremely helpful and ive been waiting for the loops for a while. glad to finally get to it now! 

Eugenio Rogriguez says:

love your videos, you should do the summary of some videos, and add challenges

【 W E A S E L F I L M S 】 says:

3:54 It could also be represented as bomb_timer -= 1. Nice video.

Nilesh Sukalikar says:

Hey man , i am very much clear about all the concepts , but one i want to ask you , how much difficult would it be for me to switch from c# to ruby . currently i am working as a asp.net developer. completed 2 years of experience. Will it affect my salary package ??

Joe Naughton says:

Hi Jake,

Im confused regarding this code on the loop do:

no = 100
loop do
  no -= 1
  next if no % 2 != 0
  puts “#{no}”
  break if no <= 0 end Here is the reasoning as I understand it: (1) sets no to 100 (2) do the following (3) -1 from no to  give 99 (4) next(I dont know what this next is for).  If our no (99) does not have a remainder of zero after being divided by 2 (it doesnt in this case because the remainder is 1) then it outputs the number.  But wouldnt this output be 99? Whenever I run the program it outputs 98, 96 and all the other even numbers. Please can you clarify?

Trell CEO says:

Please go over how to use rails framework for development

Gripnok says:

You call “while” a method, but I believe “while” is a token.

M Sousae says:

Thanks for posting the video! This is great.

Adam Nelson says:

for those of you who are used to c, c++ etc. a do-while loop in c is a begin-end while in ruby. ex:
hp = 30                       
begin
  hp -= 2
  puts “Damage done, hp remaining #{hp}”
end while hp > 0
puts “Game Over”
 

Collin Bowman says:

So the loop goes to the next even number because the modulo is followed by “!=0”, meaning the odd number spits back a modulo that is not equal to zero, pushing the program to the next even number?

Write a comment

*

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.


Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!

Send this to a friend

▷ Other ReviewsVehicles▷ Show Cars▷ Motorbikes▷ Scooters▷ Rims & Tires▷ Luxury BoatsFashion▷ Sunglasses▷ Luxury Watches▷ Luxury Purses▷ Jeans Wear▷ High Heels▷ Perfumes▷ Jewellery▷ Cosmetics▷ Shaving Helpers▷ Fashion HatsFooding▷ Chef Club▷ Fooding Helpers▷ Coktails & LiquorsSports▷ Sport Shoes▷ Fitness & Detox▷ Golf Gear▷ Racquets▷ Diving Equipment▷ Ski Gear▷ Snowboards▷ Surf Boards▷ Rollers & SkatesEntertainment▷ DIY Guides▷ Zik Instruments▷ Published Books▷ Music Albums▷ Cine Movies▷ Trading Helpers▷ Make Money▷ Fishing Equipment▷ Paintball Supplies▷ Trading Card Games▷ Telescopes▷ Knives▷ VapesHigh Tech▷ Flat Screens▷ Tech Devices▷ Camera Lenses▷ Printers▷ USB Devices▷ PC Hardware▷ Network Gear▷ Cloud Servers▷ Software Helpers▷ Programmer Helpers▷ Mobile Apps▷ Hearing AidsHome▷ Home Furniture▷ Home Appliances▷ Beddings▷ Floor Layings▷ Barbecues▷ Aquarium Gear▷ Safe Boxes▷ Office Supplies▷ Security Locks▷ Cleaning ProductsKids▷ Baby Strollers▷ Child Car Seats▷ Remote ControlledTravel▷ Luggages & Bags▷ Airlines Seats▷ Hotel Rooms▷ Fun Trips▷ Cruise Ships▷ Mexico Tours