From: Robert Klemme Date: 2010-10-06T16:52:14+09:00 Subject: Re: confused: instances or arrays? On Wed, Oct 6, 2010 at 6:52 AM, Jeremy Bopp wrote: > On 10/05/2010 09:34 PM, Paul Roche wrote: >> Hi. I want to create the following class....... >> >> Client >> >> attributes >> name, amount_wealth, loans, lender, bankrupt (a boolean) >> >> methods >> >> is_client_solvent? (I declare the client bankrupt if total amount of >> loans is greater than wealth) > > The is_client_solvent? method is therefore equivalent to the bankrupt > attribute, correct?  If so, one of these names can be an alias for the > other: > > def my_method >  "In my_method" > end > alias :another_name :my_method > > another_name                   # => "In my_method" > > In general, there isn't much need to be strict about attributes vs. > methods in Ruby.  From outside a class, all of your attributes will > operate like methods, possibly with a little syntactic sugar available > depending on how you declare them. > >> amount_owed (amount owed to the lender) >> >> lender_owed (the amount owed to the lender) > > As you've already discovered, the above part is pretty limiting... > >> I have a few questions about this.... >> >> >> it's possible for a client to have more than one lenders. Do I just >> create muliple instances eg.. >> >> client1 = new Client("Joe Bloggs", 50000, 1000, "USA Bank", nil) >> client2 = new Client("Joe Bloggs", 50000, 3000, "Chicago Bank", nil) >> >> or do I use arrays? > > That solution won't scale well (for both code and performance), and > depending on how you implement it, it could lead to wildly unmanageable > code. > >> Is it correct to use 2 different instance names for client "Joe Bloggs"? >> i.e client1 and client2 > > The definition of "correct" is ultimately up to you and the logic you > need to implement.  In other words, there is more than one way to do it. >  However, this way would not be advisable either.  For instance, how > would you handle a random assortment of a random number of clients read > in from a file at runtime? > >> If I use many instances, how do I get the aggregate of loans of each >> bank for a specific client? > > Don't use many instances per client.  Instead, think nested objects. I'd rather say "object relationships" because that is the more general concept. "Nesting" sounds like "aggregation" which is already a specific way to relate objects: http://en.wikipedia.org/wiki/Object_composition#Aggregation good explanation > I hope I wasn't too vague here, but I also don't want to deny you the > chance to derive the details for yourself. :-) For this I find the CRC approach quite helpful. It's simple and focuses on the main important aspects without distracting the designer by having too much detail: http://en.wikipedia.org/wiki/Class-Responsibility-Collaboration_card IIRC there are even software tools around for this but I find the manual paper and pencil approach much better since software tools usually also have a certain level of distraction built in. :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/