FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Output without <%= (Rails, Ruby, ERB)





adhoc
The standard way to output stuff in, say, an .rhtml file is <%= something %> -- but how do you output without that? Is there a function or something I can call?

Code:

<%
  output something
%>


Silly question, I know, I know.. I can't figure it out.
Kaneda
adhoc wrote:
The standard way to output stuff in, say, an .rhtml file is <%= something %> -- but how do you output without that? Is there a function or something I can call?

Code:

<%
  output something
%>


Silly question, I know, I know.. I can't figure it out.


There are several - the simplest ones are: puts, print, printf().

"puts" adds a linebreak at the end - works like this:
Code:
puts 'Hello world!'
puts 'Good to see you'


which outputs:

Hello world!
Good to see you

"print" doesn't add a linebreak:
Code:
print 'Hello world!'
print 'Good to see you'


outputs:

Hello world!Good to see you

Like in PHP, there's a difference between using singlequotes (') and doublequotes ("). Doublequotes make Ruby parse variables directly in your string (like "My variable is #{myvar}") as well as allowing escape codes for, say, unprintable characters (like newline - "\n"). Singlequotes ignore this, and hence are faster (Ruby doesn't need to parse the string) and should be used unless you need the other features.

"printf()" works like the function in C.
adhoc
Well, I should have said that I've tried the obvious. It doesn't print anything with those functions - it simply acts as if they weren't there. Thanks for trying, Kaneda.

<%= some_collection.inspect %>

... prints a lot of stuff for each object.

<%
puts some_collection.inspect
%>

... prints nothing.

<%
print some_collection.inspect
%>

... prints nothing.

<%
printf some_collection.inspect
%>

... prints nothing.
Marston
Kaneda wrote:
Like in PHP, there's a difference between using singlequotes (') and doublequotes (").
Actually, there is no difference, aside from the fact that you have to escape apostrophes and quotation marks respectivley from your print statements.
Kaneda
Marston wrote:
Kaneda wrote:
Like in PHP, there's a difference between using singlequotes (') and doublequotes (").
Actually, there is no difference, aside from the fact that you have to escape apostrophes and quotation marks respectivley from your print statements.


Actually, there is a difference (but I already said that, didn't I?).

Code:
x = 0
puts "x is #{x}\n"


outputs:
x is 0 <newline>

Code:
x = 0
puts 'x is #{x}\n'


outputs:
x is #{x}\n

There ya go. It's the same for PHP, Perl - and Ruby. And it's not up for debate. Razz
Related topics
PHP OOP Tutorial
Can frames be a good choice? What then?
Problem login out
[official] The Meaning of life
php problem
creating forms using SQL?
Need help fixing my php file
What do you think about Python ?
A very small PHP contest
PHP page turning problem
Is it a bad practice?
How do you measure decibels in Earphones?
Toshiba 4S Reactors, Super Safe Small Simple
A Python Expression Evaluator
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.