Hi Nathan,

how about this solution? Refactoring in general can bring you to different
directions depending on the information you know about the domain or
features that are in progress. This would be my personal view of the
domain, but I must say that I don't know what the other pieces of the
application are.

# looks like you need a class that represents your ScoreTable domain object
# which also lets the client from being agnostic of the infrastructure
underneath:
# You are using a ruby lib to display the table. I think this should be
hidden
# from the client. If at some stage you want to change the table to be an
html doc
# you only need to change ScoreTable class or use a Template pattern to
support
# multiple table formats.
class ScoreTable
  def initialize(board, columns_map)
    @board = board
    @columns_map = columns_map
  end

  def render
    rows = []
    rows << head_row
    rows << :separator

    unless @board.stats.nil?
      @board.stats.each do |stat|
      rows << body_row(stat)
      end
    end

    table = Terminal::Table.new rows: rows
    table.align_column(1, :center)
    table.align_column(2, :center)
  end

  private

  def head_row
    ['Name', 'Attempts', 'Time!']
  end

  # the order of the columns will be dictated by the head_row
  # so you don't have positional duplication.
  def body_row(stat)
    head_row.map { |column| stat[@columns_map.fetch(column)] }
  end
end

# your view_score method will become simpler and we have remove
# lower level of abstractions: e.g. dealing with Terminal::Table
# and its internals.
def view_score
  @board = Scoreboard.new
  show(msg: '- The Best 3 SCORE -')

  # I believe that passing the mapping of the columns (given the
  # fact that keys may be different) is a client's responsibility.
  # client, depending on where the data is being fetched from.
  columns_map = {
    'Name'     => :name,
    'Attempts' => :tries,
    'Time!'    => :time
  }

  @score_table = ScoreTable.new(@board, columns_map)
  @score_table.render
end


Good luck! ;)

Fabio



*Fabio Pitino*

On 7 December 2016 at 12:04, Nathan Guilty <ruby / e-solutions.re> wrote:

> Hi,
>
> Does someone can help me to refactor this piece of code :
>
>  18   def view_score
>  19     @board = Scoreboard.new
>  20     show(msg: '- The Best 3 SCORE -')
>  21     rows = []
>  22     rows << ['Name', 'Attempts', 'Time!']
>  23     rows << :separator
>  24     @board.stats.each { |s| rows << [s[:name], s[:tries], s[:time]] }
> unless     @board.stats.nil?
>  25     table = Terminal::Table.new rows: rows
>  26     table.align_column(1, :center)
>  27     table.align_column(2, :center)
>  28     puts "#{table}\n\n"
>  29   end
>
> Thank you very much for your replies.
>
> Regards,
>
> /Nathan
>
> Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>
(supressed text/html)
Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>