Why objects of class Range in Ruby don't work in descending order? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why objects of class Range in Ruby don't work in descending order?

In developing my app, I found out that objects of class Range don't work in descending order. For example: ('1'..'8').each or ('a'...'h').each works fine, but ('8'..'1').each or ('h'...'a').each don't work at all! Is it should be or is it a mistake? https://code.sololearn.com/cTSx2PcXiFBq

25th Apr 2018, 9:53 PM
meloman K_Y
meloman K_Y - avatar
8 Answers
+ 8
🇳🇬Brains You do not really need the for cycle. Well, in Ruby they are not really used, even tools like Rubocop give you warnings to use them. 10.downto(0) { |x| puts x }
25th Apr 2018, 11:33 PM
Mickel
Mickel - avatar
+ 4
Decrementing loops from what i know cannot be done with ranges: for i in (10).downto(0) puts i end or (10).downto(0).each do|x| puts x end
25th Apr 2018, 10:21 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 2
Brain are you here ? I need ur help
25th Apr 2018, 10:24 PM
Stéphane Salim
Stéphane Salim - avatar
+ 2
Lol. Brains Can we talk privately?
25th Apr 2018, 10:27 PM
Stéphane Salim
Stéphane Salim - avatar
+ 2
Ok. Pass me your mail or other.
25th Apr 2018, 10:31 PM
Stéphane Salim
Stéphane Salim - avatar
+ 2
desc = Array.new ('a'..'z').to_enum(:reverse_each).each { |alpha| desc << alpha } desc.each { |alpha| puts "letter : #{alpha}"}
25th Apr 2018, 11:19 PM
MO ELomari
+ 1
Mohamed Elomari Thanks for the specified way of the decision of this problem.
26th Apr 2018, 8:40 AM
meloman K_Y
meloman K_Y - avatar
0
Mohamed Elomari I have found another similar way, which is shorter by the number of letters. Perhaps it will be interesting to you. desc = [] ('a'..'z').to_a.reverse.each { |x| desc << x}
26th Apr 2018, 11:07 AM
meloman K_Y
meloman K_Y - avatar