I am trying to call some code that is in my lib/ directory and it ain''t working. I have a class in lib/ called ''GeographyFactory''. This is stored in a file called geography_factory.rb: class GeographyFactory ... snip end I am trying to use this in a controller. My controller looks like this: class Admin::EventController < Admin::AdminAreaController require GeographyFactory ... end When I run this I get an error: cannot convert Class into String ?? I can''t find any documentation on what I need to do to get this to work and searching the list with gmane is an exercise in futility. Can someone help? Thx.
Try ''include GeographyFactory'' instead. Require is used to get a file, include does the class. Since everything in /lib is automagically required by Rails on each refresh (assuming your in development mode), you just need to include the class. On 8/9/05, Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> wrote:> I am trying to call some code that is in my lib/ directory and it ain''t > working. > > I have a class in lib/ called ''GeographyFactory''. This is stored in a file > called geography_factory.rb: > > class GeographyFactory > ... snip > end > > I am trying to use this in a controller. > > My controller looks like this: > > class Admin::EventController < Admin::AdminAreaController > require GeographyFactory > ... > end > > When I run this I get an error: > > cannot convert Class into String > > ?? > > I can''t find any documentation on what I need to do to get this to work and > searching the list with gmane is an exercise in futility. > > Can someone help? > > Thx. > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- - Aaron ''Jomdom'' Ransley - Web: www.jomdom.net - Mail: jomdom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Thanks. Changing that does do something - a new error message! wrong argument type Class (expected Module) Hmmmm... Any ideas?> From: Aaron Ransley <jomdom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > Reply-To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Date: Tue, 9 Aug 2005 16:48:36 -0700 > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Subject: Re: [Rails] Code in lib/ > > Try ''include GeographyFactory'' instead. > > Require is used to get a file, include does the class. Since > everything in /lib is automagically required by Rails on each refresh > (assuming your in development mode), you just need to include the > class. > > On 8/9/05, Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> wrote: >> I am trying to call some code that is in my lib/ directory and it ain''t >> working. >> >> I have a class in lib/ called ''GeographyFactory''. This is stored in a file >> called geography_factory.rb: >> >> class GeographyFactory >> ... snip >> end >> >> I am trying to use this in a controller. >> >> My controller looks like this: >> >> class Admin::EventController < Admin::AdminAreaController >> require GeographyFactory >> ... >> end >> >> When I run this I get an error: >> >> cannot convert Class into String >> >> ?? >> >> I can''t find any documentation on what I need to do to get this to work and >> searching the list with gmane is an exercise in futility. >> >> Can someone help? >> >> Thx. >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > - Aaron ''Jomdom'' Ransley > - Web: www.jomdom.net > - Mail: jomdom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
It seems like a class should work but I decided to try this anyway... Still no go. wrong argument type Class (expected Module) All I did was change: class GeographyFactory to module GeographyFactory And reload the page... My GeographyFactory has two class methods that spit out arrays of hashes from data in the class itself... Really basic. I want to use them in my controller. Seems like it should be simple but it simply isn''t working.> From: Aaron Ransley <jomdom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > Date: Tue, 9 Aug 2005 16:52:16 -0700 > To: Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> > Subject: Re: [Rails] Code in lib/ > > Turn the class into a module! > > I believe the module and class syntax is exactly the same, but instead > of writing ''class'' you write ''module''. > > But I''m not sure how stuff becomes mixed in to the including class... > Modules are typically used''> Ack! Damn sausage fingers. As I was saying, modules are typically used > to do mixins, so the including class has all of the methods defined in > the module... That may or may not be what you are going for... Anyone > care to chime in on this one?> > On 8/9/05, Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> wrote: >> Thanks. >> >> Changing that does do something - a new error message! >> >> wrong argument type Class (expected Module) >> >> Hmmmm... Any ideas? >> >> >>> From: Aaron Ransley <jomdom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> >>> Reply-To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>> Date: Tue, 9 Aug 2005 16:48:36 -0700 >>> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>> Subject: Re: [Rails] Code in lib/ >>> >>> Try ''include GeographyFactory'' instead. >>> >>> Require is used to get a file, include does the class. Since >>> everything in /lib is automagically required by Rails on each refresh >>> (assuming your in development mode), you just need to include the >>> class. >>> >>> On 8/9/05, Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> wrote: >>>> I am trying to call some code that is in my lib/ directory and it ain''t >>>> working. >>>> >>>> I have a class in lib/ called ''GeographyFactory''. This is stored in a file >>>> called geography_factory.rb: >>>> >>>> class GeographyFactory >>>> ... snip >>>> end >>>> >>>> I am trying to use this in a controller. >>>> >>>> My controller looks like this: >>>> >>>> class Admin::EventController < Admin::AdminAreaController >>>> require GeographyFactory >>>> ... >>>> end >>>> >>>> When I run this I get an error: >>>> >>>> cannot convert Class into String >>>> >>>> ?? >>>> >>>> I can''t find any documentation on what I need to do to get this to work and >>>> searching the list with gmane is an exercise in futility. >>>> >>>> Can someone help? >>>> >>>> Thx. >>>> >>>> >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>> >>> >>> >>> -- >>> - Aaron ''Jomdom'' Ransley >>> - Web: www.jomdom.net >>> - Mail: jomdom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > > > -- > - Aaron ''Jomdom'' Ransley > - Web: www.jomdom.net > - Mail: jomdom-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Check the pickaxe book for how mixins work... _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I will do that... But back to my original issue - shouldn''t what I''m doing work? Why an I getting these errors?> From: Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> > Organization: Ardis Marketing Group > Reply-To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Date: Wed, 10 Aug 2005 00:33:46 -0400 > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Subject: Re: [Rails] Code in lib/ > > Check the pickaxe book for how mixins work... > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
You dont include a class in another class... "you can include a module within a class definition. When this happens, all the module''s instance methods are suddenly available as methods in the class as well" You include modules not classes if you have a class defined in a file in lib, you can just use that class after is has been required ( for example if the class is in the file ''abc_123.rb'' ) require abc_123 Hunter Hillegas wrote:>I will do that... But back to my original issue - shouldn''t what I''m doing >work? > >Why an I getting these errors? > > > > >>From: Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> >>Organization: Ardis Marketing Group >>Reply-To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>Date: Wed, 10 Aug 2005 00:33:46 -0400 >>To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>Subject: Re: [Rails] Code in lib/ >> >>Check the pickaxe book for how mixins work... >> >> >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Turns out this was a result of my Ruby newbie-ness and not realizing that WebBrick wasn''t auto-reloading the stuff in lib/. All is well now.> From: Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> > Organization: Ardis Marketing Group > Reply-To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Date: Wed, 10 Aug 2005 01:02:26 -0400 > To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> > Subject: Re: [Rails] Code in lib/ > > You dont include a class in another class... > > "you can include a module within a class definition. When this happens, > all the module''s instance methods are suddenly available as methods in > the class as well" > > You include modules not classes > > if you have a class defined in a file in lib, you can just use that class > after is has been required ( for example if the class is in the file > ''abc_123.rb'' ) > > require abc_123 > > > > Hunter Hillegas wrote: > >> I will do that... But back to my original issue - shouldn''t what I''m doing >> work? >> >> Why an I getting these errors? >> >> >> >> >>> From: Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> >>> Organization: Ardis Marketing Group >>> Reply-To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>> Date: Wed, 10 Aug 2005 00:33:46 -0400 >>> To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>> Subject: Re: [Rails] Code in lib/ >>> >>> Check the pickaxe book for how mixins work... >>> >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
If you manually introduce the files to rails using "require_dependency ''file''" they will be included in the auto reload. On 8/10/05, Hunter Hillegas <lists-HAWAbpnI61OZ1JSuHaJ1sQC/G2K4zDHf@public.gmane.org> wrote:> Turns out this was a result of my Ruby newbie-ness and not realizing that > WebBrick wasn''t auto-reloading the stuff in lib/. > > All is well now.-- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://typo.leetsoft.com - Open source weblog engine http://blog.leetsoft.com - Technical weblog
Hunter Hillegas wrote:>Turns out this was a result of my Ruby newbie-ness and not realizing that >WebBrick wasn''t auto-reloading the stuff in lib/. > >All is well now. > > > >>From: Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> >>Organization: Ardis Marketing Group >>Reply-To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>Date: Wed, 10 Aug 2005 01:02:26 -0400 >>To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>Subject: Re: [Rails] Code in lib/ >> >>You dont include a class in another class... >> >>"you can include a module within a class definition. When this happens, >>all the module''s instance methods are suddenly available as methods in >>the class as well" >> >>You include modules not classes >> >>if you have a class defined in a file in lib, you can just use that class >>after is has been required ( for example if the class is in the file >>''abc_123.rb'' ) >> >>require abc_123 >> >> >> >>Hunter Hillegas wrote: >> >> >> >>>I will do that... But back to my original issue - shouldn''t what I''m doing >>>work? >>> >>>Why an I getting these errors? >>> >>> >>> >>> >>> >>> >>>>From: Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> >>>>Organization: Ardis Marketing Group >>>>Reply-To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>>>Date: Wed, 10 Aug 2005 00:33:46 -0400 >>>>To: <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> >>>>Subject: Re: [Rails] Code in lib/ >>>> >>>>Check the pickaxe book for how mixins work... >>>> >>>> >>>>_______________________________________________ >>>>Rails mailing list >>>>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>>http://lists.rubyonrails.org/mailman/listinfo/rails >>>> >>>> >>>> >>>> >>>_______________________________________________ >>>Rails mailing list >>>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >>> >>> >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >Use rails specific ''require_dependency'' instead of ''require'' so rails knows it needs to reload the file when in development mode. require_dependency ''geography_factory'' class Admin::EventController < Admin::AdminAreaController geofac = GeographyFactory.new ... end