tutorial net
*********WOOOOOOW *********
We're happy to see you're enjoying our races (already 5 pages viewed today)! You can keep checking out by becoming a member of the tutorial net community. It's free!
You will also be able to keep track of your race progress, practice on exercises, and chat with other members.

Share
Go down
Admin
Posts : 207
Join date : 2017-11-11
Age : 27
Location : algeria
View user profilehttp://www.tutorial-net.com

tutorial ruby - Make beautiful curls with iterators

on Sun Apr 01, 2018 5:46 pm
Work with Ruby files!
So far, Ruby commands have been executed directly in the console using the IRB. Now we are going to write Ruby commands in a separate file, and then execute that file in the console. I propose you to:

create an empty file boucles.rb ;

open it in your text editor next to your console;

place your console in the folder that contains your file boucles.rb (for that type cd nom_du_dossier in your console).

Take this habit of opening a new empty file and placing your console in its folder, we will do it at the beginning of each chapter!

The loops on the boards
The rehearsal loops
In summary

To run a Ruby file from the console, place your console in the directory containing the file and type:

ruby nom_du_fichier.rb
A loop makes it possible to perform repetitive actions in a simple manner using the keyword each , for example to browse a table or repeat the same action several times.

Example of a loop to browse an array:  
Code:
jours_ouvres = [
  "lundi","mardi","mercredi","jeudi","vendredi"
]

i=5

jours_ouvres.each do |jour|
  if jour == "vendredi"
    puts jour + " : Bon weekend !"
  elsif jour == "lundi"
    puts jour + " : Bon courage !"
  else
    puts jour + " : Weekend dans #{i} jours !"
  end
  i-=1
end
Result in the console:
Code:
lundi : Bon courage !
mardi : Weekend dans 4 jours !
mercredi : Wekeend dans 3 jours !
jeudi : Weekend dans 2 jours !
vendredi : Bon weekend !
Example of a repeat loop:
Code:
7.times do
  puts "tourner sa langue"
end
puts "... et parler !"
Result in the console:
Code:
tourner sa langue
tourner sa langue
tourner sa langue
tourner sa langue
tourner sa langue
tourner sa langue
tourner sa langue
... et parler !
Back to top
Permissions in this forum:
You cannot reply to topics in this forum