<p>I have this line in a javascript block in a page:</p>

<pre><code>res = foo('&lt;%= @ruby_var %&gt;'); 
</code></pre>

<p>What is the best way to handle the case where <code>@ruby_var</code> has a single-quote in it? Else it will break the JavaScript code.</p>

```
```
@ruby_var.gsub(/[']/, '\\\\\'')
```
```
That will escape the single quote with an apostrophe, keeping your Javascript safe!
Also, if you're in Rails, there are a bunch of
Javascript-specific tools
.