I have two ruby classes for storing some custom logs in a game.
RoundLog contains information related to a single round and BattleLog is just an array that contains multiple RoundLog elements.
class RoundLog
attr_writer :actions, :number
def initialize(number)
@number = number
@actions = []
end
def to_a
[@number, @actions]
end
...
end
class BattleLog
attr_accessor :rounds
def initialize
@rounds = []
end
def print
@rounds.each do |round|
round.to_a
end
end
...
end
If I have the following BattleLog instance:
report = [#<RoundLog:0x00000008ab8328 @number=1, @actions=["Rat hits Test and deals 1 points of damage", "Test hits Rat and deals 1 points of damage"]>,
#<RoundLog:0x00000008acc170 @number=2, @actions=["Rat hits Test and deals 1 points of damage", "Test hits Rat and deals 1 points of damage"]>,
#<RoundLog:0x00000008aef5f8 @number=3, @actions=["Rat hits Test and deals 1 points of damage", "Test hits Rat and deals 1 points of damage"]>,
#<RoundLog:0x00000008b02978 @number=4, @actions=["Rat hits Test and deals 1 points of damage", "Test hits Rat and deals 1 points of damage"]>,
#<RoundLog:0x00000008b1a280 @number=5, @actions=["Rat hits Test and deals 1 points of damage"]>]
Then the following piece of code is not working: report.each {|x| x.to_a}
Instead of returning properly formatted information like that:
[1, ["Rat hits Test and deals 1 points of damage", "Test hits Rat and deals 1 points of damage"],
[2, ["Rat hits Test and deals 1 points of damage", "Test hits Rat and deals 1 points of damage"], ...]
It returns the whole RoundLog object:
[#<RoundLog:0x00000008ab8328 @number=1, @actions=["Rat hits Test and deals 1 points of damage", "Test hits Rat and deals 1 points of damage"]>,
#<RoundLog:0x00000008acc170 @number=2, @actions=["Rat hits Test and deals 1 points of damage", "Test hits Rat and deals 1 points of damage"]>,...]
However, if I try something like this: report.first.to_a it's properly returning [1, ["Rat hits Test and deals 1 points of damage", "Test hits Rat and deals 1 points of damage"]
Any idea what is wrong with my code ?
I tried renaming the to_a to something else, so I don't think the problem is with function name. It's my first question on SO, so please be indulgent.