0

I use Ruby 2.3.

I have code like this, and I want to refactor it and remove result variable.

  def place_empty_seats num
    result=''
    num.times do
      result << "<li class='seat non_active'></li>"
    end
    result.html_safe
  end

Unfortunately, I do not know the best way to look up refactoring questions besides StackOverflow. At shis moment I have no idea how to make it better, and I'll be very pleased for your input.

Thank you!

Mike Belyakov
  • 1,355
  • 1
  • 13
  • 24
  • Besides Ilya answer, for more complicated cases, you could use ```map``` method, as described for example in this answer http://stackoverflow.com/questions/12084507/what-does-the-map-method-do-in-ruby – Oleksii M Feb 13 '16 at 21:57

1 Answers1

2

You can write like:

 def place_empty_seats(num)
    ("<li class='seat non_active'></li>" * num).html_safe
 end

in your case.

Ilya
  • 13,337
  • 5
  • 37
  • 53