Inverse of eval()?

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 :wink:

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

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

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

@obj.class.to_s

Cheers,

-DF

On 7/5/06, Bill W. [email protected] 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

Hi Daniel,

Daniel N wrote:

On 7/5/06, Bill W. [email protected] 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

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

On 7/6/06, Bill W. [email protected] 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.

On 7/6/06, Daniel N [email protected] 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.