Hi, I am struggling with the problem: I want to extend Ruby''s Time class to add 3 new methods (Mayan Calendar calculations). I can do it by placing time.rb in lib/ directory class Time def first_method .. end end However, I noticed that doing so I lost access to some of Ruby''s Time extensions (eg. parse and httpdate methods) Looks like Rails parses my time.rb but rejects to parse Ruby''s time.rb extension library. I also tried require ''time'' class Time .. end and class Time class << Time .. end end but no results. I am wondering what the solution is. Karol
> However, I noticed that doing so I lost access > to some of Ruby''s Time extensions (eg. parse and httpdate > methods) > Looks like Rails parses my time.rb but rejects to parse Ruby''s time.rb > extension library.I think the following happens: Rails includes the lib directory in the include-paths. Now when the time.rb-File ist required, Ruby first finds your File (instead of Ruby''s own version) and includes this. So you are not extending but replacing time.rb! I had the same problem when extending date.rb. Simple solution: call the file something different (for example ''time_modified.rb'' and include a require ''time_modified''-Line in your environment.rb. This worked fine for me. Michael
don''t call your extensions ''time.rb'' call instead something like ''time_ext.rb'' On 6/30/05, Karol Hosiawa <hosiawak-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > I am struggling with the problem: > I want to extend Ruby''s Time class to add 3 new methods > (Mayan Calendar calculations). > > I can do it by placing time.rb in lib/ directory > class Time > def first_method > .. > end > end > However, I noticed that doing so I lost access > to some of Ruby''s Time extensions (eg. parse and httpdate > methods) > Looks like Rails parses my time.rb but rejects to parse Ruby''s time.rb > extension library. > I also tried > > require ''time'' > class Time > .. > end > > and > > class Time > class << Time > .. > end > end > > but no results. > I am wondering what the solution is. > > Karol > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Michael Raidel wrote:>> However, I noticed that doing so I lost access >>to some of Ruby''s Time extensions (eg. parse and httpdate >>methods) >>Looks like Rails parses my time.rb but rejects to parse Ruby''s time.rb >>extension library. >> >> > >I think the following happens: Rails includes the lib directory in the >include-paths. Now when the time.rb-File ist required, Ruby first >finds your File (instead of Ruby''s own version) and includes this. So >you are not extending but replacing time.rb! I had the same problem >when extending date.rb. > >Simple solution: call the file something different (for example >''time_modified.rb'' and include a require ''time_modified''-Line in your >environment.rb. This worked fine for me. > >I out all my extensions to classes like... lib/ext/time.rb so it gets ext/time instead of time and time still gets loaded and i get to keep the name nice and neat _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> I out all my extensions to classes like... > > lib/ext/time.rb > > so it gets ext/time instead of time and time still gets loaded and i get > to keep the name > nice and neat >Thanks guys, I put my time.rb in lib/ext and it solved the problem. Karol