# Embedded Ruby in Views in Ruby 1.8.7

**URL:** <https://www.ruby-forum.com/t/embedded-ruby-in-views-in-ruby-1-8-7/174913>\
**Category:** Rails\
**Created:** [September 3, 2009, 11:22pm UTC](https://www.ruby-forum.com/t/embedded-ruby-in-views-in-ruby-1-8-7/174913 "2009-09-03T23:22:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nroose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/41988e/32.png) [@nroose](https://www.ruby-forum.com/u/nroose)\
**Post date:** [September 3, 2009, 11:22pm UTC](https://www.ruby-forum.com/t/embedded-ruby-in-views-in-ruby-1-8-7/174913/1 "2009-09-03T23:22:56Z")

</div>

Good morning!

I am trying to find full documentation about the way to embed ruby  
commands in the rails views.

I am working on a large existing app. We just installed Ruby 1.8.7 on  
one of our servers in an attempt to get cruisecontrol.rb working with  
metrics\_fu.

It seems that this version of ruby does not like comments in the  
embedded ruby in the rails views.

So I am looking through the app, and there are various things in our  
views, such as:

\<%= # comment about %\>  
\<%# comment about stuff around this line %\>  
\<%= # commented out code %\>  
\<% end # comment about what it is ending %\>

And the new ruby does not seem to like any of these. It seems as  
though the %\> gets commented out as well.

There is also one other thing I could not find documentation for:

\<%- -%\>

Notice the dashes! Do they do anything? Or is it just that you can  
put dashes and it won’t really affect anything (besides perhaps the  
return value.

Thanks!

---

<div class="post-metadata">

**Author:** ![nroose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/41988e/32.png) [@nroose](https://www.ruby-forum.com/u/nroose)\
**Post date:** [September 3, 2009, 11:24pm UTC](https://www.ruby-forum.com/t/embedded-ruby-in-views-in-ruby-1-8-7/174913/2 "2009-09-03T23:24:42Z")

</div>

On Sep 3, 8:13 pm, nroose [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> embedded ruby in the rails views.
> 
> So I am looking through the app, and there are various things in our  
> views, such as:
> 
> \<%= # comment about %\>  
> \<%# comment about stuff around this line %\>  
> \<%= # commented out code %\>  
> \<% end # comment about what it is ending %\>

erb in ruby 1.8.7 slightly changed behaviour of what had been  
previously undefined behaviour (ie it just happened to work before but  
never documented as such), namely

\<%# is ok  
but \<% bla bla #  
and variants thereof aren’t

> And the new ruby does not seem to like any of these. It seems as  
> though the %\> gets commented out as well.
> 
> There is also one other thing I could not find documentation for:
> 
> \<%- -%\>
> 
> Notice the dashes! Do they do anything? Or is it just that you can  
> put dashes and it won’t really affect anything (besides perhaps the  
> return value.

it suppresses (usually) spurious blank lines.

Fred

---

<div class="post-metadata">

**Author:** ![nroose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/41988e/32.png) [@nroose](https://www.ruby-forum.com/u/nroose)\
**Post date:** [September 3, 2009, 11:27pm UTC](https://www.ruby-forum.com/t/embedded-ruby-in-views-in-ruby-1-8-7/174913/3 "2009-09-03T23:27:37Z")

</div>

On Thu, Sep 3, 2009 at 12:13 PM, nroose[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It seems that this version of ruby does not like comments in the  
> embedded ruby in the rails views.

Correct. Comments in ERB tags only worked by accident – not a feature.

> So I am looking through the app, and there are various things in our  
> views, such as:
> 
> \<%= # comment about %\>  
> \<%# comment about stuff around this line %\>  
> \<%= # commented out code %\>  
> \<% end # comment about what it is ending %\>
> 
> And the new ruby does not seem to like any of these. It seems as  
> though the %\> gets commented out as well.

\<%# is the comment tag; it works fine. The others will not.

> There is also one other thing I could not find documentation for:
> 
> \<%- -%\>
> 
> Notice the dashes! Do they do anything? Or is it just that you can  
> put dashes and it won’t really affect anything (besides perhaps the  
> return value.

\<%- strips leading whitespace

-%\> strips trailing whitespace, including newline

These are useful for precise formatting in plain text emails, for  
example.

Best,  
jeremy

---

<div class="post-metadata">

**Author:** ![nroose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/41988e/32.png) [@nroose](https://www.ruby-forum.com/u/nroose)\
**Post date:** [September 4, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/embedded-ruby-in-views-in-ruby-1-8-7/174913/4 "2009-09-04T12:15:07Z")

</div>

Thanks!

I guess I have some work to do…

---

<div class="post-metadata">

**Author:** ![nroose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/41988e/32.png) [@nroose](https://www.ruby-forum.com/u/nroose)\
**Post date:** [September 4, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/embedded-ruby-in-views-in-ruby-1-8-7/174913/5 "2009-09-04T12:47:19Z")

</div>

This will work:

\<%= ruby\_code # commented\_out\_code  
%\>

You will have to put closing tag ( “%\>”) in next line. Although this is  
a  
hack (and not by design).

–  
Thanks,  
Abhinav,  
[http://twitter.com/abhinav](http://twitter.com/abhinav)

---

<div class="post-metadata">

**Author:** ![nroose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/41988e/32.png) [@nroose](https://www.ruby-forum.com/u/nroose)\
**Post date:** [September 4, 2009, 1:06pm UTC](https://www.ruby-forum.com/t/embedded-ruby-in-views-in-ruby-1-8-7/174913/6 "2009-09-04T13:06:38Z")

</div>

2009/9/4 Abhinav S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> This will work:
> 
> \<%= ruby\_code # commented\_out\_code  
> Â %\>
> 
> You will have to put closing tag ( “%\>”) in next line. Although this is a  
> hack (and not by design).

I don’t think you could rely on that working in all future releases.

Colin

---

<div class="post-metadata">

**Author:** ![nroose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/41988e/32.png) [@nroose](https://www.ruby-forum.com/u/nroose)\
**Post date:** [September 4, 2009, 1:05pm UTC](https://www.ruby-forum.com/t/embedded-ruby-in-views-in-ruby-1-8-7/174913/7 "2009-09-04T13:05:27Z")

</div>

2009/9/4 nroose [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks!
> 
> I guess I have some work to do…

You may be able to come with a regexp that you can use for a global  
search/replace.  
You possibly want to replace all \<%somecode#somecomment%\> with  
\<%somecode%\>\<%#somecomment%\> though there will no doubt be some  
special cases.

Colin
