Martin, thanks! Let me add some to the discussion. Notes are in-line.
Some suggestions based on your traveller module:
1. Rather than inspecting the members of Character within Traveller.write,
have methods #to_text, #to_csv, #to_html etc. within the Character class,
returning string representations. Then Traveller.write would be:
def Traveller.write(c, mode):
case mode:
when 'text':
puts c.to_text
when 'csv':
puts c.to_csv
# etc.
Furthermore, mode = 'hash' does not belong in the 'write' method since it
returns a hash and does not write anything, whereas all the others write to
stdout but return nothing.
What I wasn't sure about was the HTML output. If there's a method in the
Class then how do you allow for text formatting if the web page wants to do
bold or other text formatting? Not all characters will have all fields, and
not all use cases will need all data.
2. If you define skills = Hash.new(0) you can remove the
Traveller.add_skill method altogether and just say skills[skill] += level.
Demonstration:
> a = Hash.new(0)
=> {}
> a['foo'] += 10
=> 10
> a
=> {"foo"=>10}
> a['foo'] += 10
=> 20
> a
=> {"foo"=>20}
Ah, much nicer! Thanks!
3. Refactor Traveller.first_name and Traveller.last_name:
a. have a get_name_from_db(table) method
b. rewrite your Traveller.name method to be
def Traveller.make_name(gender)
first_name_table = "humaniti_#{gender}_first"
last_name_table = "humaniti_last"
first_name = Traveller.get_name_from_db(first_name_table)
last_name = Traveller.get_name_from_db(last_name_table)
return "#{first_name} #{last_name}"
end
Let me ponder this. There are eventually supposed to be several more tables
for names, based on culture. Some cultures have very different naming
schemes.
4. Traveller.roll_dice and Traveller.upp should not be using instance
variables.
Thanks! I'll fix this shortly.
···
On Thu, Jul 7, 2016 at 8:45 PM, Martin DeMello <martindemello@gmail.com> wrote:
--
Mind on a Mission <http://leamhall.blogspot.com/>