tutorial ruby - Make beautiful curls with iterators
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:
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
- Code:
lundi : Bon courage !
mardi : Weekend dans 4 jours !
mercredi : Wekeend dans 3 jours !
jeudi : Weekend dans 2 jours !
vendredi : Bon weekend !
- Code:
7.times do
puts "tourner sa langue"
end
puts "... et parler !"
- Code:
tourner sa langue
tourner sa langue
tourner sa langue
tourner sa langue
tourner sa langue
tourner sa langue
tourner sa langue
... et parler !
Permissions in this forum:
You cannot reply to topics in this forum