# What's the "#" for?

**URL:** <https://www.ruby-forum.com/t/whats-the-for/50944>\
**Category:** Ruby\
**Created:** [December 23, 2005, 1:13am UTC](https://www.ruby-forum.com/t/whats-the-for/50944 "2005-12-23T01:13:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![redrhodes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@redrhodes](https://www.ruby-forum.com/u/redrhodes)\
**Post date:** [December 23, 2005, 1:13am UTC](https://www.ruby-forum.com/t/whats-the-for/50944/1 "2005-12-23T01:13:07Z")

</div>

I’ve started on my first ruby project and came accross the following  
code from:  
[http://habtm.com/articles/2005/11/13/your-site-doesnt-look-too-good-in-x-browser](http://habtm.com/articles/2005/11/13/your-site-doesnt-look-too-good-in-x-browser)

Since I’ve managed to understand most of what’s going on here, but I  
still can’t find a reference on how and why to use “#”. From  
following the code it looks like an escape character for a variable to  
be populated when enclosed within quotes. Does anyone have enough  
understanding on the syntax to help a ruby nubie?

class Hash  
def to\_sql  
sql = keys.sort {|a,b| a.to\_s\<=\>b.to\_s}.inject([[]]) do |arr, key|  
arr[0] \<\< “#{key} = ?”  
arr \<\< self[key]  
end  
[sql[0].join(’ AND ')] + sql[1…-1]  
end  
end

Thanks,  
Richard

---

<div class="post-metadata">

**Author:** ![redrhodes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@redrhodes](https://www.ruby-forum.com/u/redrhodes)\
**Post date:** [December 23, 2005, 1:34am UTC](https://www.ruby-forum.com/t/whats-the-for/50944/2 "2005-12-23T01:34:12Z")

</div>

On 12/22/05, redrhodes [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Hash  
> def to\_sql  
> sql = keys.sort {|a,b| a.to\_s\<=\>b.to\_s}.inject([[]]) do |arr, key|  
> arr[0] \<\< “#{key} = ?”  
> arr \<\< self[key]  
> end  
> [sql[0].join(’ AND ')] + sql[1…-1]  
> end  
> end

a = “hay guys”  
puts “this is what a is: #{ a }”

prints:  
this is what a is: hay guys

---

<div class="post-metadata">

**Author:** ![redrhodes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@redrhodes](https://www.ruby-forum.com/u/redrhodes)\
**Post date:** [December 23, 2005, 1:43am UTC](https://www.ruby-forum.com/t/whats-the-for/50944/3 "2005-12-23T01:43:10Z")

</div>

Just wondering, what language has been your main programming language  
until now?

I’m always curious to know where the influx of new Ruby programmers are  
coming from. I’m from a C++/C#/.Net background and while the syntax is  
similar to both C++ and C# some things are definitely a bit different.

Jeff

---

<div class="post-metadata">

**Author:** ![redrhodes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@redrhodes](https://www.ruby-forum.com/u/redrhodes)\
**Post date:** [December 23, 2005, 3:28am UTC](https://www.ruby-forum.com/t/whats-the-for/50944/4 "2005-12-23T03:28:30Z")

</div>

Joe and Ryan, thanks for the examples and reference. I’ll take a  
closer looke at the pickaxe in the future.

Af for my previous programming experience: I’ve been a jack of all  
trades for a while. But, I have the most experience in Perl, Python,  
and C.

Richard

---

<div class="post-metadata">

**Author:** ![redrhodes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@redrhodes](https://www.ruby-forum.com/u/redrhodes)\
**Post date:** [December 23, 2005, 1:43am UTC](https://www.ruby-forum.com/t/whats-the-for/50944/5 "2005-12-23T01:43:13Z")

</div>

On Dec 22, 2005, at 7:32 PM, Joe Van D. wrote:

> > be populated when enclosed within quotes. Does anyone have enough  
> > end  
> > end
> 
> a = “hay guys”  
> puts “this is what a is: #{ a }”
> 
> prints:  
> this is what a is: hay guys

From the Ruby.new chapter in Programming Ruby : The Pragmatic  
Programmer’s Guide ([http://whytheluckystiff.net/ruby/pickaxe/](http://whytheluckystiff.net/ruby/pickaxe/))

"The second thing that Ruby does with double-quoted strings is  
expression interpolation. Within the string, the sequence #  
{ expression } is replaced by the value of expression. We could use  
this to rewrite our previous method.

def sayGoodnight(name)  
result = “Goodnight, #{name}”  
return result  
end

When Ruby constructs this string object, it looks at the current  
value of name and substitutes it into the string. Arbitrarily complex  
expressions are allowed in the #{…} construct."
