# Inverse of eval()?

**URL:** <https://www.ruby-forum.com/t/inverse-of-eval/97065>\
**Category:** Rails\
**Created:** [July 4, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/inverse-of-eval/97065 "2006-07-04T18:18:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 4, 2006, 6:18pm UTC](https://www.ruby-forum.com/t/inverse-of-eval/97065/1 "2006-07-04T18:18:16Z")

</div>

I’m trying to figure out how to turn a string into an object. Can  
anyone point me? Here’s some background.

## Here’s the part of what I’m trying to do that works.

@ford = [‘mustang’, ‘pinto’]  
make = ‘@ford’  
puts eval(make)

## –\> produces mustang pinto

What I can’t figure out how to do is something like this…

* * *

make = ‘@chevy’  
assign(make) = [‘camaro’, ‘vette’]  
puts eval(make)

–\> What I need this to produce is  
camaro  
vette

Which of course it doesn’t 😉

I’m still pretty new to Ruby and, while I’m sure there’s got to be an  
inverse to eval(), the hours I’ve spent in the Pickaxe book on this  
haven’t clicked for me.

Any assistance is greatly appreciated!!!

Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 4, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/inverse-of-eval/97065/2 "2006-07-04T22:53:46Z")

</div>

On Jul 4, 2006, at 9:15 AM, Bill W. wrote:

> mustang  
> –\> What I need this to produce is
> 
> Bill

Bill-

```
Hey can you give a better example of what you are trying to do? What

```

kind of object should “camaro” and “vette” become? Are they names of  
your model classes? Give me a little more info about what you are  
trying to do and Ill help.

-Ezra

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 4, 2006, 11:33pm UTC](https://www.ruby-forum.com/t/inverse-of-eval/97065/3 "2006-07-04T23:33:49Z")

</div>

You’re wanting to print the class name? You can do that with:

@obj.class.to\_s

Cheers,

-DF

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 5, 2006, 4:24am UTC](https://www.ruby-forum.com/t/inverse-of-eval/97065/4 "2006-07-05T04:24:36Z")

</div>

On 7/5/06, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> → produces
> 
> Any assistance is greatly appreciated!!!

not 100% sure what your after but looking at your code something like

make = ‘chevy’  
instance\_variable\_set( make.to\_sym, [‘camaro’,‘vette’] )  
puts eval( “@#{make}”)

should do what you seem to be trying to do. rather than eval’ing tho  
you  
could use

puts instance\_varaible\_get( make.to\_sym )

Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 5, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/inverse-of-eval/97065/5 "2006-07-05T16:00:20Z")

</div>

Hi Daniel,

Daniel N wrote:

> On 7/5/06, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> I’m trying to figure out how to turn a string into an object.  
> Can anyone point me? Here’s some background.

> not 100% sure what your after but looking at your code  
> something like
> 
> make = ‘chevy’  
> instance\_variable\_set( make.to\_sym, [‘camaro’,‘vette’] )  
> puts eval( “@#{make}”)
> 
> should do what you seem to be trying to do.

Thanks! I gave it a try in the console and its exactly what I was  
looking  
for. I’d tried the to\_sym, but not the instance\_variable\_set. I’d  
found it  
in the Pickaxe but didn’t understand (still don’t) the example code.  
Will  
work with it some more now that you’ve gotten me started.

## Just fyi, the syntax above is slightly off. For future readers…

## make = ‘@chevy’ instance\_variable\_set(make.to\_sym,[‘camaro’, ‘vette’] puts eval(make)

## puts eval(make) —\> produces camaro vette

## puts @chevy —\> produces camaro vette

Thanks again!

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 5, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/inverse-of-eval/97065/6 "2006-07-05T16:51:03Z")

</div>

Hi Ezra,

Ezra Z. wrote:

> Hey can you give a better example of what you are  
> trying to do?

In general, what I was trying to do is to create an object where the  
name of  
the object is defined by the value of a string variable. See Daniel N’s  
reply and mine back for a good answer to that question. I was able to  
solve  
the larger problem I was dealing with by creating an instance variable  
that’s a hash of hashes. It worked, but I think Daniel’s approach would  
have been cleaner. Here’s a simplified explanation of the larger  
problem I  
was working.

Assume an Inventory table. Each record is for one inventory item; in  
this  
case a vehicle.  
Assume a seperate array (@makes) of the different makes of vehicle my  
business carries. (e.g., @makes = [‘ford’, ‘chevy’] )

In the view, I present seperate

s for each make of vehicle.  
If there are no Inventory records for a make, I present the user with  
one
. If there are Inventory records, I present the user with a different
, one that contains the records for that make.

In the view, I want to do …

\<% @makes.each {|this\_make| %\>  
\<% make = ‘@’ + this\_make %\>  
\<% if eval(make) == nil %\>  
\<%= render :partial =\> ‘no\_inventory’ %\>  
\<% else %\>  
\<% @inventory = eval(make) %\>  
\<%= render :partial =\> ‘have\_inventory’,  
:locals =\> {:make =\> this\_make} %\>  
\<% end %\>  
\<% } %\>

In the view above (and in \_have\_inventory.rhtml in order to render the  
list)  
I need access to the instance variable containing the inventory records  
for  
that make. The problem was how to construct that instance variable in  
the  
controller. It looks like I can do it with instance\_variable\_set.

I’d be very interested in hearing any other ideas on how to approach  
this  
general problem.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 5, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/inverse-of-eval/97065/7 "2006-07-05T17:04:14Z")

</div>

On 7/6/06, Bill W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the object is defined by the value of a string variable. See Daniel N’s  
> Assume a seperate array (@makes) of the different makes of vehicle my
> 
> In the view above (and in \_have\_inventory.rhtml in order to render the  
> list)  
> I need access to the instance variable containing the inventory records  
> for  
> that make. The problem was how to construct that instance variable in the  
> controller. It looks like I can do it with instance\_variable\_set.
> 
> I’d be very interested in hearing any other ideas on how to approach this  
> general problem.

Best regards,

> Bill

One other way Bill, is to have make as a model in your app, with it’s  
own  
table. Then have something like vehicle\_models attached to the  
vehicle\_make

eg

class VehicleMake \< ActiveRecord::Base  
has\_many :vehicle\_models  
  
end

class VehicleModel \< ActiveRecord::Base  
belongs\_to :vehicle\_make  
  
end

In your controller

def list\_makes  
@makes = VehicleMake.find(:all)  
end

Then in your view

\<% @makes.each do |this\_make| %\>  
\<% if this\_make.vechile\_models.count \< 1 %\>  
\<%= render :partial =\> ‘no\_inventory’ %\>  
\<% else %\>  
\<%= render :partial =\> ‘have\_inventory’, :locals =\> {:make =\>  
this\_make}  
%\>  
\<% end %\>

Might not be what your after… Just a thought.

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [July 5, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/inverse-of-eval/97065/8 "2006-07-05T17:07:39Z")

</div>

On 7/6/06, Daniel N [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > trying to do?  
> > > have been cleaner. Here’s a simplified explanation of the larger  
> > > If there are no Inventory records for a make, I present the user with  
> > > \<%= render :partial =\> ‘no\_inventory’ %\>  
> > > for
> 
> \<% end %\>

\<% end %\>

Sorry Missed an \<% end %\>

Might not be what your after… Just a thought.
