Forum: Ruby on Rails Whats the difference between "<%= abc %>" and "<%= abc -%>"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
55cfa29d393bc3cf2a1f2d373c85126e?d=identicon&s=25 sultan (Guest)
on 2009-03-18 17:13
(Received via mailing list)
What difference does the "-" at the end make?
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-03-18 17:28
(Received via mailing list)
> What difference does the "-" at the end make?

-%> eats the newline.

.<%= abc %><%= xyz %>. will turn into

.abc
xyz
.

.<%= abc -%><%= xyz -%>. will turn into

.abcxyz.

-philip
55cfa29d393bc3cf2a1f2d373c85126e?d=identicon&s=25 sultan (Guest)
on 2009-03-18 18:27
(Received via mailing list)
Thanks Philip, that makes sense.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-03-18 22:01
(Received via mailing list)
2009/3/18 Philip Hallstrom <philip@pjkh.com>

>
> .<%= abc -%><%= xyz -%>. will turn into
>
> .abcxyz.
>

According to Agile Development with Rails this is not quite correct.
The -
is supposed to remove the newline _after_ the -%>
So
<%= "abc" %><%= "def" %>
<%= "ghi" %><%= "klm" %>

will provide

abcdef
ghijkl

but
<%= "abc" %><%= "def" -%>
<%= "ghi" %><%= "klm" %>

will provide

abcdefghijkl

However on testing this in Rails 2.2.2 and viewing the source of the
page, I
am not seeing this. The - seems to make no difference, I see the two
line
output in both cases.

Can anyone elucidate?

Colin
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2009-03-18 22:31
(Received via mailing list)
>> xyz
> So
> <%= "ghi" %><%= "klm" %>
>
> Can anyone elucidate?

Ah... the -%> is only "end of line".  Didn't realize that.

I just plugged this into 2.2.2:

<%= "abc" %><%= "def" %>
<%= "ghi" %><%= "klm" %>

--------

<%= "abc" %><%= "def" -%>
<%= "ghi" %><%= "klm" %>

And got back this:

abcdef
ghiklm

--------

abcdefghiklm
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-03-18 22:31
(Received via mailing list)
On Wed, Mar 18, 2009 at 4:00 PM, Colin Law <clanlaw@googlemail.com>
wrote:
>> .
>
>
> abcdefghijkl
>
> However on testing this in Rails 2.2.2 and viewing the source of the page, I
> am not seeing this. The - seems to make no difference, I see the two line
> output in both cases.
>
> Can anyone elucidate?
>
> Colin


Chances are your web server supports some sort of output compression,
so all this doesn't really matter much.  Surely you don't depend on
newlines to properly display a page, right?



--
Greg Donald
http://destiney.com/
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-03-18 22:46
(Received via mailing list)
2009/3/18 Philip Hallstrom <philip@pjkh.com>

> --------
>
> abcdefghiklm
>
>
Which is what you should get, I tried exactly the same and got

abcdef
ghiklm

--------

abcdef
ghiklm

Which is not correct.   Following a little head scratching I realised
that
that the erb file had windows line endings (\r\n) but I am now working
on
Ubuntu.  I changed the line endings to Unix format and I got
abcdefghiklm in
the second case as expected.

Is this a reportable bug?  should the -%> not remove \r\n if present?
Colin
55cfa29d393bc3cf2a1f2d373c85126e?d=identicon&s=25 sultan (Guest)
on 2009-03-19 18:39
(Received via mailing list)
Is the -%> only used with a <%= ?
Can it be used with, for example <% end -%>
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-03-19 19:19
(Received via mailing list)
2009/3/19 sultan <sultan.bhatia@gmail.com>

>
> Is the -%> only used with a <%= ?
> Can it be used with, for example <% end -%>
>

Yes it can, in fact that is possibly the main use as it removes the line
completely from the source. Also useful with  'for' and so on.
Colin
This topic is locked and can not be replied to.