Tom Tom - 1 year ago 59
Ruby Question

Enter string repeatedly Ruby Cucumber

I have a test that includes character lengths within fields etc.

I was wondering if I could have a set string of 10 characters like

str = 'abcdefghij'

then have it multiply that string by the amount of times needed to fulfil the character length and fill in the field.

I've tried the
method but that just enters the same value over x iterations.

What I want is to take str, increase it ten fold and enter that value as 1 continuous string so

I'd parameterize the number of times to increase it depending on the field I'm testing. I want to do this so that I don't have huge amounts of variables stored to satisfy each test.

Can this be done? I hope I've explained clearly.

Answer Source

String#* would do:

'abc' * 10
#⇒ "abcabcabcabcabcabcabcabcabcabc"

To use a floating point parameter:

λ = ->(input, count) do
  i, f = *count.divmod(1)
  input * i << input[0...(f * input.size).to_i]

λ.('abcd', 2.5)
#⇒ 'abcdabcdab'