1

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.

1 Answer 1

4

Using map instead of each should solve your problem.

each runs some operations inside the block and then returns the object/array/hash/enumerable/whatever that each was called upon. map however returns a new array with the return values calculated in your block.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it's working :) I really missed that important difference between each and map (I thought the problem is somewhere else).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.