I have 3 classes ("Truck", "Semi", "Motorcycle") that all inherit from the "Vehicle" class. Each subclass has an attribute for "Wheels" that tells how many wheels the vehicle has (I do not store these values in the database because they will never change) I''m wondering what is the preferred method for handling this? Right now I have individual methods in each class for "Wheels": For example: class Truck def wheels 4 end end class Motorcycle def wheels 2 end end class Semi def wheels 18 end end Is there a better way??? Thanks -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d1fc7e559a885e015f3369f37ac34838%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Maybe you have a particular limited set of vehicles but trucks I know have from 4 to 10 (or more) wheels and motorcycles have 2 or 3 wheels and Semi-trailer trucks have 10 to 20 (or more) wheels depending on application. I would store the number of wheels in the database. Norm On 05/24/2013 11:40 PM, oliver bee wrote:> I have 3 classes ("Truck", "Semi", "Motorcycle") that all inherit from > the "Vehicle" class. > Each subclass has an attribute for "Wheels" that tells how many wheels > the vehicle has (I do not store these values in the database because > they will never change) > > I''m wondering what is the preferred method for handling this? Right now > I have individual methods in each class for "Wheels": > > For example: > > class Truck > def wheels > 4 > end > end > > class Motorcycle > def wheels > 2 > end > end > > class Semi > def wheels > 18 > end > end > > Is there a better way??? Thanks >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/51A105F0.50100%40earthlink.net?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Thanks for the Response Norm, That isn''t necessary in this instance though (all trucks will have 4 wheels). Should I just keep with what I have? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ce37b7e60715961828a03b76c3c08562%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
On Sat, May 25, 2013 at 11:21 PM, oliver bee <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks for the Response Norm, That isn''t necessary in this instance > though (all trucks will have 4 wheels). Should I just keep with what I > have?I agree with Norm that this feels inauthentic - objects are typically used to ''model'' real world things and relationships, and I can see lots of exceptions on your horizon :-) In any case, I would probably do something like: class Vehicle attr_accessor :wheel_count end class Truck < Vehicle def initialize(wheel_count = 4) @wheel_count = wheel_count end end ... and so on. I''d also recommend ''Practical Object-Oriented Design in Ruby'' ( http://www.poodr.info/ ) for some good thoughts on this. FWIW, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yA%2B5_R9ThVGwj03DFsdSCwj3eP3WhXkubfO4b7qWZ4a2Q%40mail.gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
I originally tried to set it up that way and it works until I retrieve a record from the database. I dont think it triggers the initialize function. because I cant call the "wheels" method on an object after I retrieve it. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bc26fb7846a60717b50986cdfdf657f0%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Hassan Schroeder wrote in post #1110193:> On Sat, May 25, 2013 at 11:21 PM, oliver bee <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>Nice one!> class Vehicle > attr_accessor :wheel_count > end > > class Truck < Vehicle > def initialize(wheel_count = 4) > @wheel_count = wheel_count > end > end > > ... and so on. > > I''d also recommend ''Practical Object-Oriented Design in Ruby'' > ( http://www.poodr.info/ ) for some good thoughts on this.-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9ee24901128ebd99793fc8325452b5d4%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
On 05/25/2013 11:21 PM, oliver bee wrote:> Thanks for the Response Norm, That isn''t necessary in this instance > though (all trucks will have 4 wheels). Should I just keep with what I > have? >What you suggested would work. My approach would be to have a vehicle class/table with the characteristics of the vehicle types just because I think STI is more bother than reward unless there are other things that you do not mention that would drive you in that direction. I prefer the simplest approach that will do what is needed. It makes it easier to pass it on to someone else and I am not as likely to forget why things are like they are. Norm -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/51A2BFA2.6050100%40earthlink.net?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
On Sun, May 26, 2013 at 11:24 AM, oliver bee <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I originally tried to set it up that way and it works until I retrieve a > record from the database.Sorry, my bad for not explicitly saying that the approach above does imply you''re going to persist that attribute (which makes sense if the value is variable). -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCiDodvfs6eeQmXV%2Bg6PQ60jauOkz%3DmRnW36TGYdmhJYg%40mail.gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.