4

How do I descent from 5 to 1 instead of going from 1 to 5? I have this...

<div class="field-container rating">
  <% (5..1).each do |i| %>
  <%= f.radio_button :rating, i, :id => "star#{i}" %>
  <% end %>
</div>

(1..5) goes from 1, 2, 3, 4, 5. What is the correct way of going 5, 4, 3, 2, 1?

2 Answers 2

10

You can do downto

<div class="field-container rating">
  <% 5.downto(1) do |i| %>
  <%= f.radio_button :rating, i, :id => "star#{i}" %>
  <% end %>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Ah Ruby...this is way better than the Pythonesque suggestion I was about to make (setting a variable, decrementing it in a while loop).
very simple and easy! Thank you!
0

You can use to_a.reverse

for i in (1..5).to_a.reverse
   puts i
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.