Arrays 创建数组选项的所有排列时出现的问题
不确定我在这里遗漏了什么,尝试创建一个新的数组,其中包含大小和颜色数组的所有可能选项。将此用作指南:。所需输出应为:黑色L、黑色M、黑色S、黑色XL、黑色XXL、红色L、红色M、红色S、红色XL、红色XXLArrays 创建数组选项的所有排列时出现的问题,arrays,if-statement,permutation,capybara-webkit,Arrays,If Statement,Permutation,Capybara Webkit,不确定我在这里遗漏了什么,尝试创建一个新的数组,其中包含大小和颜色数组的所有可能选项。将此用作指南:。所需输出应为:黑色L、黑色M、黑色S、黑色XL、黑色XXL、红色L、红色M、红色S、红色XL、红色XXL require "capybara/dsl" require "spreadsheet" require "fileutils" require "open-uri" include Capybara::DSL Capybara.run_server = false Capybara.d
require "capybara/dsl"
require "spreadsheet"
require "fileutils"
require "open-uri"
include Capybara::DSL
Capybara.run_server = false
Capybara.default_driver = :selenium
Capybara.default_selector = :xpath
Spreadsheet.client_encoding = 'UTF-8'
visit "http://www.example.com/sexy-women-pencil-dress-see-through-mesh-stripes-backless-bodycon-slim-party-clubwear-g0376.html"
if page.has_selector?("//dd[1]//select[contains(@name, 'options')]//*[@price='0']") and page.has_no_xpath?("//dd[2]//select[contains(@name, 'options')]//*[@price='0']")
optionchoice = page.all("//dd[1]//select[contains(@name, 'options')]//*[@price='0']")
options = optionchoice.collect(&:text).join(', ')
elsif page.has_selector?("//dd[2]//select[contains(@name, 'options')]//*[@price='0']") and page.has_no_xpath?("//dd[1]//select[contains(@name, 'options')]//*[@price='0']")
optionchoice = page.all("//dd[2]//select[contains(@name, 'options')]//*[@price='0']")
options = optionchoice.collect(&:text).join(', ')
else page.has_selector?("//dd[1]//select[contains(@name, 'options')]//*[@price='0']") and page.has_selector?("//dd[2]//select[contains(@name, 'options')]//*[@price='0']")
colorchoice = page.all("//dd[1]//select[contains(@name, 'options')]//*[@price='0']")
colors = colorchoice.collect(&:text).join(', ')
sizechoice = page.all("//dd[2]//select[contains(@name, 'options')]//*[@price='0']")
sizes = sizechoice.collect(&:text).join(', ')
combinedchoice = [[colors],[sizes]]
options = combinedchoice.each.product(*combinedchoice[1..-1]).map(&:join)
end
puts options
假设您有两个数组、颜色和大小:
colors = ["Black", "Red"]
sizes = ["L", "M", "S", "XL", "XXL"]
要打印包含所有产品的阵列(中间带斜杠):
类型:
下次删除不必要的代码,并给出明确的输入。
["Black-L","Black-M","Black-S","Black-XL","Black-XXL","Red-L","Red-M","Red-S","Red-XL","Red-XXL"]
p colors.product(sizes).map { |x| x.join("-") }