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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060704/4b4a8abd/attachment.html
On Jul 4, 2006, at 9:15 AM, Bill Walton wrote:> 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!!! > > BillBill- 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060704/17c05d34/attachment.html
You''re wanting to print the class name? You can do that with: @obj.class.to_s Cheers, -DF On 7/5/06, Bill Walton <bill.walton@charter.net> wrote:> > > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
On 7/5/06, Bill Walton <bill.walton@charter.net> wrote:> > 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'' <%27@ford%27> > puts eval(make) > > --> produces > mustang > pinto > ----------------------------------- > > What I can''t figure out how to do is something like this... > > ---------------------------------- > make = ''@chevy'' <%27@chevy%27> > 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!!! >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> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060705/d097e7b8/attachment.html
Hi Daniel, Daniel N wrote:> On 7/5/06, Bill Walton <bill.walton@charter.net> 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 Zygmuntowicz 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 <div>s for each make of vehicle. If there are no Inventory records for a make, I present the user with one <div>. If there are Inventory records, I present the user with a different <div>, 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 Walton <bill.walton@charter.net> wrote:> > Hi Ezra, > > Ezra Zygmuntowicz 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 <div>s for each make of vehicle. > If there are no Inventory records for a make, I present the user with > one > <div>. > If there are Inventory records, I present the user with a different > <div>, 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,> BillOne 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 <snip> end class VehicleModel < ActiveRecord::Base belongs_to :vehicle_make <snip> 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. _______________________________________________> Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060705/63bfd191/attachment.html
On 7/6/06, Daniel N <has.sox@gmail.com> wrote:> > > > On 7/6/06, Bill Walton <bill.walton@charter.net> wrote: > > > > Hi Ezra, > > > > Ezra Zygmuntowicz 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 <div>s for each make of vehicle. > > If there are no Inventory records for a make, I present the user with > > one > > <div>. > > If there are Inventory records, I present the user with a different > > <div>, 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 > > > 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 > <snip> > end > > class VehicleModel < ActiveRecord::Base > belongs_to :vehicle_make > <snip> > 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 %> ><% end %> Sorry Missed an <% end %> Might not be what your after... Just a thought.> > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060705/e372edf5/attachment-0001.html