Alexander Abramovich
Alexander Abramovich

Reputation: 11448

Spawning an array

Not sure that my question is correct, but in general I would do the following:

Given a code snippet like:

[{ <some events>, "close"}, { <some other events>, "close"}].each do |events|
    it "should handle events" do
        ...
    end
    ...
end

I would like to augment the events' array. Say there are 2 ways to close: "close" (same) and "stop". Which means, I would like to write some code that will check 4 sequences: [{<some events>, "close"}, {<some other events>, "close"}, {<some events>, "stop"}, {<some other events>, "stop"}].

What would be the proper (well-styled in Ruby) way to code it?

Update: the <some events> and <some other events> are sequences of strings (this is just to clarify).

Update N2: stop and close in a more general case can appear in the middle of the sequence as well.

Update N3: it just might be that having a single final sequence at do will be more convenient. I might be wrong here.

Update N4: Example (just to make it all clear):

initial messages: "open", "click_btn1", "click_btn2", "open", "click_btn2", "click_btn3",

desired result: "open", "click_btn1", "click_btn2", "close" "open", "click_btn1", "click_btn2", "close" "open", "click_btn2", "click_btn3", "stop" "open", "click_btn2", "click_btn3", "stop"

Upvotes: 0

Views: 63

Answers (1)

sawa
sawa

Reputation: 168139

Just have nested iterations.

[some_events, some_other_events].each do |event|
  ["close", "stop"].each do |close_or_stop|
    ...
  end
end

Or maybe

[some_events, some_other_events].product(
["close", "stop"]) do |event, close_or_stop|
  ...
end

Upvotes: 2

Related Questions