【问题标题】:Converting output of one function into array and feed value into variable in other function in ruby将一个函数的输出转换为数组并将值输入到ruby中其他函数的变量中
【发布时间】:2022-01-01 03:58:55
【问题描述】:

对于 ruby​​ 编程逻辑之一,我试图将一个函数的字符串输出转换为数组,而数组必须将值提供给程序的其他函数中声明的变量

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

10.1.1.1
10.1.1.2
10.1.1.3

此输出应转换为数组并作为同一程序的单独函数中的值提供

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

所以 filter_pattern 的输出应该如下所示

   '[w1,w2,w3,w4,w5,w6!="*10.1.1.1*"&&w6!="*10.1.1.2*"&&w6!="*10.1.1.3*"]'

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: ruby aws-sdk aws-sdk-ruby


【解决方案1】:
# 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

【讨论】:

  • user1678123,但在 aws 控制台中我可以看到 w6!="#{ips[0]}"&&w6!="#{ips[1 ]}*&&w6!="*#{ips[2]}"]' 输出而不是数组中对应的 IP
  • @Kavitha 更具体一点,不清楚您的评论是什么意思
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 2022-01-12
  • 2020-10-26
  • 2019-05-20
  • 2013-04-19
相关资源
最近更新 更多