0

I have the following code block and I want to turn the output into an array rather than a series of strings, but I'm not really sure now to push that in.

default['max_log']['instances'] = 20

servers haproxy_backends('max_logger').map { |h|
  ("1".."#{node['max_log']['instances']}").each  { |i|
    "#{h}:83#{i} #{h}:83#{i} check inter 10s rise 2 fall 3"
  }
}

The servers function should be ingesting an array sort of like this where all variables are expanded and resolved.

[
  'server1:8301 server1:8301', 
  'server1:8302 server1:8302', 
  'server2:8301 server2:8301', 
  ...
]

I've tried making some really simple version of this in irb for testing, but I really just don't know what I'm doing.

a = []
# => []

def d
  ('01'..'10').each { |i| puts i }
end
# => :d

a.push(d)
01
02
03
04
05
06
07
08
09
10

# => ["01".."10"]
a

# => ["01".."10"]
d
01
02
03
04
05
06
07
08
09
10
# => "01".."10"

backends = ['maxlog-1', 'maxlog-2']
backends.map { |h| (1..5).each { |i| puts "#{h}:#{i}" } }

ruby test.rb
# => maxlog-1:1
# => maxlog-1:2
# => maxlog-1:3
# => maxlog-1:4
# => maxlog-1:5
# => maxlog-2:1
# => maxlog-2:2
# => maxlog-2:3
# => maxlog-2:4
# => maxlog-2:5
2
  • are you asking how to create an array of strings? Commented Apr 22, 2017 at 0:14
  • Yes, an array of strings based on mapping the backends array and expanding the range. Commented Apr 22, 2017 at 0:15

1 Answer 1

1

This method will take an array and append elements to it based on a range. Should give a sufficient example to do what you are trying to do.

myarray = [];

def d(out, min, max)
    (min.to_s..max.to_s).each do |idx|
        out << "my string #{idx}"
    end
end

d(myarray, 1, 10)
puts(myarray)
Sign up to request clarification or add additional context in comments.

1 Comment

I'm dumb. It looks like one of my own tests was generating a proper array, but the way it was outputting I couldn't tell. Thanks for the help.

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.