views:

860

answers:

5

I know that I can do this in Rails:

<%="hello" %>

but is there any way to do this

<%
echo "hello"
%>

and get it to show up in the response? I have tried response.write which almost worked, but did not...

Edit: print or puts do not do it, because I do not want to write to the console. I want to write to the browser/HTTP client.

Edit: Here is an example:

<%
unless @research_activities.size == 0
   concat(render(:partial => 'list'))
end
%>

Why would I want to include two closing tags just to do that? It reads nicely in code, doesn't it?

A: 

How about print "hello" or printf("hello")

Jack L.
Thanks Jack, trying to get it to the client, not to the console.
Yar
A: 

You're looking for "print" or "puts", depending on whether or not you want a newline (probably not). Almost every object implements .to_s, which works also, though my feeling is that there's probably a better way to do whatever you're trying to do. Any more context on this?

phresus
Thanks, no, I want to see it on the client, not on the console.
Yar
There's no reason why it wouldn't work on the console. <%=puts "Any Ruby code goes here" %>
phresus
-1, puts (or p) outputs to STDOUT by default, which is NOT where the response is generated. And <%= puts ... %> will produce an empty string in the response, puts (or p) return nil and nil.to_s == "".
Samuel
thanks Samuel. I've never had more incorrect answers for a question. Somewhere between what I *should* do and what I *can* do, many answerers got lost.
Yar
+4  A: 

Have you tried concat.

I have seen this when wandering in Rails documentation. Not sure at all since I am very new to Rails.

Vincent Robert
Nice shot! http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html
Yar
+2  A: 

What you have to write is

<% concat "bank", binding %>

now you can do something like

<%
  10.times do
    concat "cat", binding
  end
%>

for ten cat

Bank
Precisely. In this context it makes sense to use concat instead of <%=. Thanks for your answer.
Yar
I think explicit binding is not needed anymore...
Andrew Vit
Yeah, I had no idea what that binding was, myself.
Yar
A: 

Just curious, why would you want to write directly to the response object?

pantulis
it's just a syntax thing. Sometimes the <%= thing is cumbersome. concat whatever is sometimes neater in the middle of, for example, a for loop.
Yar