+ 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
8 Antworten
+ 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 }
+ 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
+ 2
Brain are you here ? I need ur help
+ 2
Lol. Brains Can we talk privately?
+ 2
Ok. Pass me your mail or other.
+ 2
desc = Array.new
('a'..'z').to_enum(:reverse_each).each { |alpha| desc << alpha }
desc.each { |alpha| puts "letter : #{alpha}"}
+ 1
Mohamed Elomari
Thanks for the specified way of the decision of this problem.
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}