# Syntax

**URL:** <https://www.ruby-forum.com/t/syntax/141568>\
**Category:** Rails\
**Created:** [June 27, 2008, 5:29am UTC](https://www.ruby-forum.com/t/syntax/141568 "2008-06-27T05:29:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![northwind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ad7895/32.png) [@northwind](https://www.ruby-forum.com/u/northwind)\
**Post date:** [June 27, 2008, 5:29am UTC](https://www.ruby-forum.com/t/syntax/141568/1 "2008-06-27T05:29:36Z")

</div>

Hi,  
Can someone point me to a resource to learn about syntax like this:  
“%#{search}%” I understand everything but the two percent signs. I  
tried googling but various searches on curly braces and percent signs  
brought no luck, and of course we can’t search using literal % and  
{. (As I writ this I wonder if google will do something to strip  
them out and this post will look completely ridiculous!  
Thanks.

---

<div class="post-metadata">

**Author:** ![northwind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ad7895/32.png) [@northwind](https://www.ruby-forum.com/u/northwind)\
**Post date:** [June 27, 2008, 5:41am UTC](https://www.ruby-forum.com/t/syntax/141568/2 "2008-06-27T05:41:53Z")

</div>

Umm, is that the whole syntax?

Or is it, \<% #{search} %\> ?  
In which case the \<% %\> means that it is embedded ruby code. Found in  
.rhtml files. Which means the code #{search} is evaluated and the  
value is rendered by the DOM as html.

Now \<% %\> won’t output anything, just be evaluated.

You need the equal sign, \<%= %\> to output to the viewport.

So in an .rhtml file, \<%= #{search} %\> will evaluate the search and  
return it as a string and output the value to the html to be rendered  
in the viewport.

If you need to know what #{search} does, that is ruby code. So  
learning some ruby code may help you.

HTH,  
Anita.

---

<div class="post-metadata">

**Author:** ![northwind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ad7895/32.png) [@northwind](https://www.ruby-forum.com/u/northwind)\
**Post date:** [June 27, 2008, 6:33am UTC](https://www.ruby-forum.com/t/syntax/141568/3 "2008-06-27T06:33:32Z")

</div>

> Hi,  
> Can someone point me to a resource to learn about syntax like this:  
> “%#{search}%” I understand everything but the two percent signs. I  
> tried googling but various searches on curly braces and percent signs  
> brought no luck, and of course we can’t search using literal % and  
> {. (As I writ this I wonder if google will do something to strip  
> them out and this post will look completely ridiculous!  
> Thanks.

I’m going to hazard a guess that the entire line looks something like  
this:

search = params[:query]  
MyModel.find(:all, :conditions =\> “title LIKE %#{search}%”)

? If so, the #{} is a ruby construct that interpolates what’s inside  
it for double quoted strings. The %'s on either end is SQL’s wildcard.

If you do see that string above as-is, change it since the above  
doesn’t escape the input.  
Change it to at least this:

MyModel.find(:all, :conditions =\> [“title LIKE ?”, “%{#search}%”])

-philip

---

<div class="post-metadata">

**Author:** ![northwind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ad7895/32.png) [@northwind](https://www.ruby-forum.com/u/northwind)\
**Post date:** [June 27, 2008, 6:55am UTC](https://www.ruby-forum.com/t/syntax/141568/4 "2008-06-27T06:55:09Z")

</div>

Thank you for the replies Anita and philip,  
I was just in the process of writing a reply to Anita with a more  
complete example when philips reply came through.  
It is the SQL wildcard.

# models/product.rb

def self.search(search, page)  
paginate :per\_page =\> 5, :page =\> page,  
:conditions =\> [‘name like ?’, “%#{search}%”], :order =\>  
‘name’  
end

The two %'s straddling the Ruby construct inside the double quotes  
made me think it was some Ruby or Rails syntax. BTW I tried googling  
around sql but again it is difficult to search around punctuation and  
symbols.  
Thanks for your time.  
Scott.

---

<div class="post-metadata">

**Author:** ![northwind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ad7895/32.png) [@northwind](https://www.ruby-forum.com/u/northwind)\
**Post date:** [June 27, 2008, 9:43am UTC](https://www.ruby-forum.com/t/syntax/141568/5 "2008-06-27T09:43:08Z")

</div>

Yes, this also applies . The “#{variable}” syntax is Ruby’s string  
interpolation of a variable. It would be like print “$variable” in  
Perl. -J.

---

<div class="post-metadata">

**Author:** ![northwind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ad7895/32.png) [@northwind](https://www.ruby-forum.com/u/northwind)\
**Post date:** [June 27, 2008, 9:43am UTC](https://www.ruby-forum.com/t/syntax/141568/6 "2008-06-27T09:43:08Z")

</div>

If you’ve seen “%#{variable}%”, in a find statement, that’s a SQL  
fragment and the percent signs work very much like a \* sign in a bash  
(or cmd) shell. Search for “LIKE Operator” on this page:

[http://www.firstsql.com/tutor2.htm](http://www.firstsql.com/tutor2.htm)

-J.
