0

I want to generate 5 buttons with different values based on one integer. For example I've got 30, I want to create buttons with 10 20 30 40 50

value = 30
int1 = value - 20
int2 = value - 10
int3 = value
int4 = value + 10
int5 = value + 20

buttoncode = ""
%w{int1 int2 int3 int4 int5}.each do |minutes|
   buttoncode += 'buttoncode'
end

I can do it in a very bad way, but it could be done a smarter solution I guess. Is it possible to make something like that?

%w{sum(max-20) sum(max-10) max sum(max+10) sum(max+20)}.each do |minutes|

end

1 Answer 1

1

See Ruby: How to iterate over a range, but in set increments?

So in your case it would be:

(min..max).step(10) do |n|
   n += 'buttoncode'
end

By the way, this is not really Rails specific, but Ruby specific. Rails is a web framework that handles the interaction between browser and the web server that is built on top of Ruby.

If you feel like you aren't that up to speed with Ruby, try https://learnrubythehardway.org/book/ and do some exercise on HackerRank or ProjectEuler in Ruby.

1
  • 1
    @RickySpanish np, ProjectEuler has definitely helped me to learn Ruby by doing, so I would recommend it to anyone who would like to practice the basics of a new programming language.
    – Mistlight
    Commented Dec 8, 2016 at 20:02

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.