# Variable variables?

**URL:** <https://www.ruby-forum.com/t/variable-variables/93955>\
**Category:** Rails\
**Created:** [April 16, 2006, 5:20am UTC](https://www.ruby-forum.com/t/variable-variables/93955 "2006-04-16T05:20:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [April 16, 2006, 5:20am UTC](https://www.ruby-forum.com/t/variable-variables/93955/1 "2006-04-16T05:20:19Z")

</div>

Hi everyone,

Does Ruby support variable variables like PHP  
([PHP: Variable variables - Manual](http://www.php.net/manual/en/language.variables.variable.php))? Or do I  
have  
to use eval, like

some\_hash.each\_pair{|key, pair|  
eval “#{key} = #{value}”  
}

Or is there some other way?

I’m actually trying to figure this out in order to use ez\_where to  
create  
its conditions from a hash, so if this isn’t even necessary could  
someone  
let me know?

Thanks,  
Daniel

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [April 16, 2006, 5:29am UTC](https://www.ruby-forum.com/t/variable-variables/93955/2 "2006-04-16T05:29:23Z")

</div>

On Apr 15, 2006, at 8:17 PM, Daniel H. wrote:

> Or is there some other way?
> 
> I’m actually trying to figure this out in order to use ez\_where to  
> create  
> its conditions from a hash, so if this isn’t even necessary could  
> someone  
> let me know?
> 
> Thanks,  
> Daniel

Daniel-

```
There is already support for hash.to_sql and other niceties included

```

in ez\_where. Like so:

class Hash #:nodoc:  
def to\_sql(param = ‘AND’)  
map { |key,value| key.to\_s+’ = ‘+ActiveRecord::Base.send  
(:sanitize, value) }.join(’ ‘+param+’ ')  
end

def to\_conditions(param = ‘AND’)  
[map { |k, v| k.to\_s+’ = ?’ }.join(’ ‘+param+’ '), \*values]  
end

alias :to\_sql\_conditions :to\_conditions  
def to\_named\_conditions(param = ‘AND’)  
[map { |k, v| k.to\_s+’ = :’+k }.join(’ ‘+param+’ '),  
self.symbolize\_keys]  
end

end

Look in lib/hash.rb for this. Also the best way to see what that  
plugin is capable of is looking at the test suite. The  
ez\_where\_tests.rb file in the plugin is 700 lines long and exercises  
all the capabilities of the plugin. So you can see many many sample  
codes using the plugin in that tests file. Its the most complete  
documentation on it right now 😉

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [April 16, 2006, 6:12am UTC](https://www.ruby-forum.com/t/variable-variables/93955/3 "2006-04-16T06:12:05Z")

</div>

Hi Ezra,

Thanks for your response!

Let’s say I’m trying to collect old gambling debts and want to find  
people  
who owe me money within a range of values, and who live in certain  
cities.  
My hash looks something like

search {  
:location\_ids =\> [0, 8, 10]  
:amount\_owed =\>  
{:from =\> 5000, :to =\> 10000}  
:still\_alive =\> ‘true’  
}

I’ve looked through the tests file and wasn’t able to gain any insight  
into  
how I could convert a hash like this into a nice string of conditions.  
I’m  
imagining something like, to start with,

cond = Caboose::EZ::Condition.new :chumps do  
search.each\_pair {|key, value|  
if defined? value[:from]  
eval “#{key} \>= #{value[:from]}”  
next  
end  
end  
end

But the more I look at that code the more ridiculous it seems. Any  
ideas?

Thanks again for your help!

Daniel

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [April 16, 2006, 11:45am UTC](https://www.ruby-forum.com/t/variable-variables/93955/4 "2006-04-16T11:45:21Z")

</div>

# One difficulty I found with Hash#to\_sql was that if values in the hash were empty you could end up with something like "seller\_id = 3 AND AND views

45"

> From what I understand, though, part of the Ruby Way is to have many little  
> method that do one thing, so I should make method that strips out all  
> empty  
> values first and pass the result to one of the new Hash methods.

I didn’t think about that until just now, though. In the mean time I  
made  
the following, which hopefully someone will find useful:

def conditions\_from\_hash(some\_hash)  
condition = ‘’  
some\_hash.each\_pair {|key, value|  
if value.empty?  
next  
elsif (value.class == Hash) && value.values.inject{|total,  
current|   
total+current}.empty?  
next  
elsif (value.class == Array)  
if value.inject{|total, current| total+current}.empty?  
next  
elsif /(.\*)\_ids$/.match(key)  
condition \<\< “#{key[0…-4].pluralize}.`id` IN(” \<\<   
value.join(’,’) \<\< ") AND "  
end  
elsif value[:from] || value[:to]  
condition \<\< "#{key} \>= #{value[:from]} AND " unless   
value[:from].empty?  
condition \<\< "#{key} \<= #{value[:to]} AND " unless  
value[:to].empty?  
else  
condition \<\< "#{key} LIKE ‘%#{value}%’ AND "  
end  
}  
condition[0…-5].to\_s  
end

It’s pretty much particular to what I’m trying to do, so it doesn’t have  
niceties like a “param” argument. What I’m trying to do is perform a  
search  
based on input which includes a range (  
and \<input search[price][to]) and a bunch of category checkboxes  
() . I wanted to keep the form of the code general  
enough  
to allow me to use it for other controllers in the site with the same  
needs.

Daniel
