tags:

views:

109

answers:

2

Is there a way I can find all the variables in Ruby that begin with a particular string? For example, I have the following variables in my ruby program

ret_d = 1

ret_d2 = 23

Now, is there a function that will return ["ret_d","ret_d2"]? The problem is that I do not have the set of all variables.

Thanks.

+4  A: 

If they're local variables, you could use local_variables. However, this sounds like a poor man's array. You might consider using an actual Array.

Chuck
Awesome. Thanks.
+10  A: 

Kernel#local_variables should do the trick.

>> ret_d = 1
=> 1
>> ret_d2 = 23
=> 23
>> local_variables
=> ["_", "ret_d", "ret_d2"]
>> local_variables.select{|v|v=~/^ret_/}
=> ["ret_d", "ret_d2"]
Burke