Hello, I am trying to find a way to preserve the same class name without having to cut and paste code from one class to another. How do I do something such as: test1.rb ---- class A def initialize echo "Initailizing class A." end end test2.rb --- require ''test1'' class A def initialize super echo "Initializing class A again." end end a = A.new Expected output: Initailizing class A Initializing class A again. I know this is not best practice, and that I should be using a different name for my child class I am deriving off the superclass, but I was just wanting to know if it is actually doable. Thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Inheritance does it nicely: class A def initalize puts "Initializing class A" end end class B < A def initialize super puts "Initalizing class B" end end On Jan 10, 2008 8:41 AM, Russell McConnachie <russell-chlIOe/xi6aw5XvHYEYAbQ@public.gmane.org> wrote:> > Hello, > > I am trying to find a way to preserve the same class name without having > to cut and paste code from one class to another. How do I do something > such as: > > test1.rb > ---- > class A > def initialize > echo "Initailizing class A." > end > end > > test2.rb > --- > require ''test1'' > > class A > def initialize > super > echo "Initializing class A again." > end > end > > a = A.new > > Expected output: > > Initailizing class A > Initializing class A again. > > I know this is not best practice, and that I should be using a different > name for my child class I am deriving off the superclass, but I was just > wanting to know if it is actually doable. > > Thanks. > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Ryan, I was actually looking at using the same class name. I didn''t think it was possible but was wondering. In ruby you can override any method as you wish dynamically. So defining class A twice with two initialize functions will not work because the second defined class of the same name will override the first. Ryan Bigg wrote:> Inheritance does it nicely: > > class A > def initalize > puts "Initializing class A" > end > end > > class B < A > def initialize > super > puts "Initalizing class B" > end > end > > > On Jan 10, 2008 8:41 AM, Russell McConnachie <russell-chlIOe/xi6aw5XvHYEYAbQ@public.gmane.org > <mailto:russell-chlIOe/xi6aw5XvHYEYAbQ@public.gmane.org>> wrote: > > > Hello, > > I am trying to find a way to preserve the same class name without > having > to cut and paste code from one class to another. How do I do something > such as: > > test1.rb > ---- > class A > def initialize > echo "Initailizing class A." > end > end > > test2.rb > --- > require ''test1'' > > class A > def initialize > super > echo "Initializing class A again." > end > end > > a = A.new > > Expected output: > > Initailizing class A > Initializing class A again. > > I know this is not best practice, and that I should be using a > different > name for my child class I am deriving off the superclass, but I > was just > wanting to know if it is actually doable. > > Thanks. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Not quite override, more like extending. Re-defining a class in Ruby extends that class, but redefining a method on a class will override that method, like what you''re finding out now. On Jan 10, 2008 9:32 AM, Russell McConnachie <russell-chlIOe/xi6aw5XvHYEYAbQ@public.gmane.org> wrote:> > Hi Ryan, > > I was actually looking at using the same class name. I didn''t think it > was possible but was wondering. In ruby you can override any method as > you wish dynamically. So defining class A twice with two initialize > functions will not work because the second defined class of the same > name will override the first. > > Ryan Bigg wrote: > > Inheritance does it nicely: > > > > class A > > def initalize > > puts "Initializing class A" > > end > > end > > > > class B < A > > def initialize > > super > > puts "Initalizing class B" > > end > > end > > > > > > On Jan 10, 2008 8:41 AM, Russell McConnachie <russell-chlIOe/xi6aw5XvHYEYAbQ@public.gmane.org > > <mailto:russell-chlIOe/xi6aw5XvHYEYAbQ@public.gmane.org>> wrote: > > > > > > Hello, > > > > I am trying to find a way to preserve the same class name without > > having > > to cut and paste code from one class to another. How do I do > something > > such as: > > > > test1.rb > > ---- > > class A > > def initialize > > echo "Initailizing class A." > > end > > end > > > > test2.rb > > --- > > require ''test1'' > > > > class A > > def initialize > > super > > echo "Initializing class A again." > > end > > end > > > > a = A.new > > > > Expected output: > > > > Initailizing class A > > Initializing class A again. > > > > I know this is not best practice, and that I should be using a > > different > > name for my child class I am deriving off the superclass, but I > > was just > > wanting to know if it is actually doable. > > > > Thanks. > > > > > > > > > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 9, 2:11 pm, Russell McConnachie <russ...-chlIOe/xi6aw5XvHYEYAbQ@public.gmane.org> wrote:> > I know this is not best practice, and that I should be using a different > name for my child class I am deriving off the superclass, but I was just > wanting to know if it is actually doable.No, there can only be one class by a given name, and it can''t derive from itself. What is your goal? ///ark --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---