I have the following array.
@arr = ["A:c,b,a", "B:a,b,d", "C:a,c,b", "D:d,a,c", "a:A,C,D", "b:D,A,B", "c:B,A,C", "d:D,C,A"]
I want to create an array with the combination of the letter before : and after :.
Output
["Ac,Ab,Aa", "Ba,Bb,Bd", "Ca,Cc,Cb", "Dd,Da,Dc", "aA,aC,aD", "bD,bA,bB", "cB,cA,cC", "dD,dC,dA"]
I have tried the following but it gives an error.
@arr.each { |item| name, choices =item.split(':')
@names<<name
@choices<<choices.strip
}
@combi = @name.product(@choices)
I appreciate any inputs. I thank you in advance.
=========
Full code
soshisoai.rb
class Soshisoai
attr_reader :names, :choices, :arr, :combi
def initialize
@arr=[]
@names=[]
@choices=[]
file = File.new('soshisoai.txt', "r")
file.each_line{|line| @arr<< line.strip }
@arr.each { |item| name, choices =item.split(':')
@names<<name
@choices<<choices.strip
}
@combi = @name.product(@choices)
end
end
soshisoai_spec.rb
require './spec_helper'
require './soshisoai.rb'
describe Soshisoai do
specify{ expect(Soshisoai.new().arr).to eq ["A:c,b,a", "B:a,b,d", "C:a,c,b", "D:d,a,c", "a:A,C,D", "b:D,A,B", "c:B,A,C", "d:D,C,A"] }
specify{ expect(Soshisoai.new().names).to eq ["A", "B", "C", "D", "a", "b", "c", "d"] }
specify{ expect(Soshisoai.new().choices).to eq ["c,b,a", "a,b,d", "a,c,b", "d,a,c", "A,C,D", "D,A,B", "B,A,C", "D,C,A"] }
specify{ expect(Soshisoai.new().combi).to eq ["Ac,Ab,Aa", "Ba,Bb,Bd", "Ca,Cc,Cb", "Dd,Da,Dc", "aA,aC,aD", "bD,bA,bB", "cB,cA,cC", "dD,dC,dA"] }
end
soshisoai.txt
A:c,b,a
B:a,b,d
C:a,c,b
D:d,a,c
a:A,C,D
b:D,A,B
c:B,A,C
d:D,C,A