0

I'm looking to create an HTML structure with classes based on the values of arrays from Ruby. I have 6 classes that will be applied to different elements on an 8x8 grid. Each row will be a div with 8 span elements inside. In ruby, each nested array will be the div row and then each element will be a span assigned a class based on the value of the array element.

a = [[1,4,3,2,2,3,1,4]
     [4,5,6,6,3,2,3,5]]

So two rows will be created with 8 elements inside with the appropriate classes. Is it possible to convert data structures to HTML like this in Ruby?

2 Answers 2

1

Maybe this is what you want:

a = [[1,4,3,2,2,3,1,4],
     [4,5,6,6,3,2,3,5]]

html = ''
a.each do |row|
  html << "<div>%s</div>" % row.map { |c| %{<span class="#{c}"></span>} }.join
end

# puts html

update

In other words:

html = a.map do |row|
  "<div>%s</div>" % row.map { |c| %{<span class="#{c}"></span>} }.join
end.join
Sign up to request clarification or add additional context in comments.

Comments

0

umm.. yea. something among the lines of...

a.each do |subArray|
  puts "<div>"
  subArray.each do |element|
    puts '<span class="#{element}">Some text</span>'
  end
  puts "</div>
end

If this doesn't fit your needs please post a more specific question.

1 Comment

puts to produce HTML? Really?

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.