简体   繁体   English

将一个 function 的 output 转换为数组并将值输入到另一个 function 中的变量

[英]Converting output of one function into array and feed value into variable in other function in ruby

For one of ruby programming logic, am trying to convert the string output of one function into an array and from array have to feed the values to the variables declared in other function of the program For one of ruby programming logic, am trying to convert the string output of one function into an array and from array have to feed the values to the variables declared in other function of the program

//response.each do |instance |
  print "#{instance.private_ip_address}"
  print "\n"
  end
//

10.1.1.1
10.1.1.2
10.1.1.3

This output should be converted into array and feed as values in separate function of same program此 output 应转换为数组并作为同一程序的单独 function 中的值提供

def run_me
    ::
    ::
    filter_pattern = '[w1,w2,w3,w4,w5,w6!="*#{array[0]}*"&&w6!="*#{array[1]}*&&w6!="*#{array[2]}*"]'

So that the output of filter_pattern, should be like below所以filter_pattern的output应该如下

   '[w1,w2,w3,w4,w5,w6!="*10.1.1.1*"&&w6!="*10.1.1.2*"&&w6!="*10.1.1.3*"]'
# Mocked responses for the sake of the example..
responses = [
  OpenStruct.new(request_type: 'GET', private_ip_address: '10.1.1.1'),
  OpenStruct.new(request_type: 'GET', private_ip_address: '10.1.1.2'),
  OpenStruct.new(request_type: 'GET', private_ip_address: '10.1.1.3')
]

data_of_ips = [] # Storing ips in this array from the loop for later usage in run_me method

responses.each do |instance|
  data_of_ips << instance.private_ip_address
end

# Now calling the run_me and pass the array as argument

run_me(data_of_ips)

# replace it in your filter like this
def run_me(ips)
  # 
  # 
  filter_pattern = '[w1,w2,w3,w4,w5,w6!="*#{ips[0]}*"&&w6!="*#{ips[1]}*&&w6!="*#{ips[2]}*"]'^Z
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM