Ruby Programming Loops
Ruby also provides different types of loops like other programming languages does. But its syntax quiet different and also multiple options you can see for the loop constructs.
Ex;2
Few challenges were found while executing this script.
factorial.rb:6:in `>': comparison of String with 0 failed (ArgumentError) from factorial.rb:6:in `'
it was troubleshooted by adding "chomp.to_i" method to gets method to convert given number into integer
Most important lesson: read the error messages. Ruby failed to compare a number to a string because they are just incompatible data types: it's nonsensical to ask what is bigger: "abc" or 123. And gets.chomp always gives you a String (something like "4", which is not the number 4).
So in order to compare the user's input to a number, you have to convert it to a number first: Ruby Strings have the .to_i (to integer) method for this
Ruby also provides different types of loops like other programming languages does. But its syntax quiet different and also multiple options you can see for the loop constructs.
While and until loops in Ruby |
While loop
Ex: Printing 1 to 10 numbersEx;2
while
#script to find factorial of a given number puts "enter the number" x=gets.chomp.to_i fact=1 while x > 0 fact= fact * x x -= 1 end puts "the factorial for given number is #{fact}"
Output: E:\Ruby>ruby factorial.rb enter the number 5 the factorial for given number is 120
Few challenges were found while executing this script.
factorial.rb:6:in `>': comparison of String with 0 failed (ArgumentError) from factorial.rb:6:in `
until
for loop
foreach loop
each doThis loop stops execution when condition becomes false
Really Good blog post.provided a helpful information.I hope that you will post more updates like this Ruby on Rails Online Training
ReplyDeleteMMORPG
ReplyDeleteInstagram Takipçi Satın Al
tiktok jeton hilesi
tiktok jeton hilesi
antalya saç ekimi
referans kimliği nedir
instagram takipçi satın al
metin2 pvp serverlar
instagram takipçi satın al
SMM PANEL
ReplyDeleteSMM PANEL
iş ilanları
İNSTAGRAM TAKİPÇİ SATIN AL
hırdavatçı burada
beyazesyateknikservisi.com.tr
Servis
tiktok jeton hilesi
kadıköy mitsubishi klima servisi
ReplyDeletekartal vestel klima servisi
ataşehir daikin klima servisi
maltepe toshiba klima servisi
pendik lg klima servisi
üsküdar vestel klima servisi
beykoz bosch klima servisi
çekmeköy samsung klima servisi
ümraniye toshiba klima servisi