14 June 2007

Why I Like Ruby

I need to see if a property of the last n elements of a collection ("moves") is nil, where n is the number of players:

 moves.last(players.size).collect(&:piece).compact.size == 0

I don't know how to do this so readably in any number of lines of code in java:

  • initialise a counter to zero
  • from (moves.size - 1) down to (moves.size - (players.size + 1)), inspect "piece" property
  • if it's not null, increment the counter
  • counter == 0