Skip to content

Instantly share code, notes, and snippets.

@tkling
Last active May 27, 2020 14:50
Show Gist options
  • Save tkling/db6f780f606ab50151a2f338996cfa33 to your computer and use it in GitHub Desktop.
Save tkling/db6f780f606ab50151a2f338996cfa33 to your computer and use it in GitHub Desktop.
dnd-companion notes
  1. using class level state is generally considered to be dangerous. (@@var type stuff) - OK for this project since it's the only thing using its own code, but if this were a gem where people could pull in your code, imagine what would happen if someone accesses the Conditions/Equipments/Spells @@all attribute and changes stuff?
  • as an alternative, you could create wrapping objects (something like EquipmentCollection) that exist just to fetch and hold all of the items, and your CLI would talk to those Collection objects to present the lists of all of the items.
  1. consider using a gemfile/gemspec (gemspec if this could be a gem) to declare your dependencies
  2. declaring an array and shoveling items into it is redundant in ruby:
    things = []
    doodads.each { |dd| things << "based god say: #{dd}" }
    
    # is equivalent to:
    
    things = doodads.map { |dd| "based god says: #{dd}" }
  3. Mapping + hash access can simplify some code:
    equipment_names = []
    equipment_array.each do |hash| 
       hash.collect do |key, value|
         if key == "name"
           equipment_names << value
         end
       end
     end
     equipment_names
     
     # is equivalent to:
     
     equipment_names = equipment_array.map do |hash|
       hash['name']
     end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment