garbagecollector
garbagecollector

Reputation: 3880

The quick way to escape characters in Ruby for ERB

I'm using ERB and trying to include a number of strings. I ran into a problem when the strings start containing questionable characters like " or \.

I looked at this SO's post: Ruby: Escaping special characters in a string

Which could solve my problem, but I have about more than a dozen variables to be templated, and calling that method to each of the variable seems a bit of a tedious job. So, before I go ahead and start calling escape all over, I'm wondering if there's a shorter way to do this?

Upvotes: 2

Views: 4153

Answers (1)

mgamba
mgamba

Reputation: 1199

%q seems to handle JSON syntax pretty well:

ruby-1.9.2-p290 :089 > %q{ ' " \ \\ \/ \b \f \n \r \t \u ^F}
# => " ' \" \\ \\ \\/ \\b \\f \\n \\r \\t \\u \u0006"

Upvotes: 2

Related Questions