class Array
def deep_array_count()
count = 0
each{|el|
#I could check with is_a?(Array), but with respond_to? you could use your own classes.
if el.respond_to?(:deep_array_count)
count += 1 + el.deep_array_count
end
}
count
end
end
x = [
[
[
["Array", "1"], ["Array", "2"] ],
[ ["Array", "3"], ["Array", "4"] ],
],
[
[ ["Array", "5"], ["Array", "6"] ],
[ ["Array", "7"], ["Array", "8"] ]
]
]
p x.deep_array_count
この例の結果は8 要求された私は、各配列を数えるあなたのない、14であるがあります。
8を取得するには、別の配列のない配列のみを数えなければなりません。
class Array
def deep_array_count()
count = 0
each{|el|
#I could check with is_a?(Array), but with respond_to? you could use your own classes.
if el.respond_to?(:deep_array_count)
i = el.deep_array_count
count += i == 0 ? 1 : i #if no other array is inside, add 1
end
}
count
end
end
x = [
[
[
["Array", "1"], ["Array", "2"] ],
[ ["Array", "3"], ["Array", "4"] ],
],
[
[ ["Array", "5"], ["Array", "6"] ],
[ ["Array", "7"], ["Array", "8"] ]
]
]
p x.deep_array_count
どのように '[[[[ "編曲"、 "1"]、[ "編曲"、 "2" について]、[["Arr"、 "3"]]]、[["Arr"、 "4"]、["Arr"、 "5"]]] '? –