explain simple example please

2 views
Skip to first unread message

theG

unread,
Aug 14, 2008, 7:57:46 PM8/14/08
to Ramaze
class MainController < Ramaze::Controller
def index
%q(
<?r @str = "abc" ?>
@str is #{@str}
)
end
end

why does the code in the midle need %q {} around it?
should the body of the function return string/html content? does the
%q mean some sort of directive?

Thanks

Adam Kittelson

unread,
Aug 14, 2008, 10:04:25 PM8/14/08
to ram...@googlegroups.com
The %q( ) is an example of a multi line string, manveru replied with this link in your other topic http://www.zenspider.com/Languages/Ruby/QuickRef.html#6 which is a good source of information on that.

It did lead me to wonder about something though. At the link above it says that %q is a multi line string with no interpolation. The example below uses interpolation, and based on the %q my expectation would be that it would fail and just output @str is #{@str} to the browser. My text editor seems to agree because it doesn't syntax highlight the interpolation.

However, when I run this example code the interpolation is parsed and my browser displays @str is abc.

Sorry to hijack your question theG, but can anyone help me understand why the interpolation still works in this example?

-Adam

Aman Gupta

unread,
Aug 14, 2008, 10:13:03 PM8/14/08
to ram...@googlegroups.com
Sorry to hijack your question theG, but can anyone help me understand why the interpolation still works in this example?

You're passing the uninterpolated version to Ramaze as a template, and ramaze will use the ezamar templating engine to render that template before sending it to the browser. 

  Aman

Adam Kittelson

unread,
Aug 14, 2008, 10:21:03 PM8/14/08
to ram...@googlegroups.com
Gotcha, so it's not being interpolated immediately... and in retrospect if it was the example wouldn't work because @str hasn't been defined until ezamar gets it's hands on it.

Thanks

James Britt

unread,
Aug 15, 2008, 1:00:02 AM8/15/08
to ram...@googlegroups.com
Adam Kittelson wrote:
> Gotcha, so it's not being interpolated immediately... and in retrospect if
> it was the example wouldn't work because @str hasn't been defined until
> ezamar gets it's hands on it.


An interesting twist here is that you *could* use an interpolated string
as a means to dynamically create the string that would be interpolated
by the Ezamar engine.

--
James Britt

www.happycamperstudios.com - Wicked Cool Coding
www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff

Reply all
Reply to author
Forward
0 new messages