Ruby Inheritance/namespacing in ruby gem

I am working on a ruby gem that faces off to a sports api. I do not fully understand classes/inheritance and am looking for some guidance.

The predicament is I have multiple sports, baseball, basketball etc. and they all have some similar overlap in resources, i.e. players, teams etc.

Currently I have it structured as follows:

module SportsApi
  module Mlb
    class Player
    end
  end
end

If I have the following, would my player class be overwritten or is it ok because it is wrapped inside of a module?

module SportsApi
  module Nfl
    class Player
    end
  end
end

Should I just have a player class that is common across all sports and then create separate MlbPlayer, NflPlayer classes that inherit from the generic player class? I feel like I kind of need a rails single table inheritance structure but now sure how I would reimplement that in this gem structure. Any help/suggestions is appreciated!

Answers


If I have the following, would my player class be overwritten or is it ok because it is wrapped inside of a module?

No, your class won't be overwritten. Except if someone does it on purpose by accessing the SportsApi::Mlb namespace.

Should I just have a player class that is common across all sports and then create separate MlbPlayer, NflPlayer classes that inherit from the generic player class?

The question is, what MlbPlayer & NflPlayer would need from Player? Would it need all of its functionality and would diverge a little from it or it would share some functionality with it?

Inheritance is considered not-so-good by many for a bunch of reasons, mostly because it fails to model the real world (where relationships are mostly "has-a" and not "is-a") so I'd suggest that you consider using Composition: http://rubysource.com/composition-inheritance/


Need Your Help

Trouble with pointers while making hashmap in C

c pointers hashmap

for a C introduction I need to make a hashmap. These are the structures that I made so far:

Query on datetime fields with milliseconds gives wrong result in SQL Server

sql sql-server-2005 datetime

I'm running into an odd bug using datetime fields in SQL Server 2005. The datetime field shows up with millisecond-level accuracy, but it looks like the milliseconds are not always used. Here's my ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.