2
range_array = (0..100).to_a
range_array.each do |number|
  p %-<div class="#{number}"><h1>Dell Shitbox</h1></div>-
end

Result:

"<div class=\"83\"><h1>Dell Shitbox</h1></div>"

Desired result:

<div class="83"><h1>Dell Shitbox</h1></div>

I have tried escaping:

range_array = (0..100).to_a
range_array.each do |class|
  p %-<div class=\"#{class}\"><h1>Dell Shitbox</h1></div>-
end

I have tried switching the quoting method (many, many times)

range_array = (0..100).to_a
range_array.each do |class|
  p %Q[<div class="#{class}"><h1>Dell Shitbox</h1></div>]
end

Suggestions?

softcode
  • 4,358
  • 12
  • 41
  • 68
  • What seems to be the problem? Is your code throwing an error? It appears to be acting as it should. – Charles Caldwell Jan 21 '15 at 02:23
  • 1
    +1 Though for a very well formed question. You presented current code, expected result, and included what you have tried. I think you are a little confused on what the code is doing but you have a well formed question. – Charles Caldwell Jan 21 '15 at 02:27
  • @CharlesCaldwell The problem is that the code is outputting a backslash (and outer double quotes). I read somewhere that the backslash doesn't exist, which I can fathom, but unfortunately HTML can't. Suggested docs? – softcode Jan 21 '15 at 02:35
  • @Anthony No it won't unfortunately – softcode Jan 21 '15 at 02:36
  • check out the pickaxe, http://ruby-doc.com/docs/ProgrammingRuby/ under Standard Types and then under the header Strings – snowe Jan 21 '15 at 02:37
  • Are you trying to write this to a `.html` file or are you using a template system like erb? – Charles Caldwell Jan 21 '15 at 02:38
  • 2
    Oh the problem is he's using `p` instead of `puts`. It outputs completely fine, but he's calling `inspect` on it with `p` and therefore everything is escaped. – snowe Jan 21 '15 at 02:38

1 Answers1

1

The problem you are seeing is because you are outputting the string using p. p is the same as calling:

puts object.inspect

The simple fix is to use puts instead of p.

snowe
  • 1,312
  • 1
  • 20
  • 41