我认为这是一个愚蠢的问题,哈哈
我有以下数组
[['a','b','c'],['d','e','f']]
并希望该数组是
['a','b','c'],['d','e','f']
这意味着我想删除第一个括号。
那有意义吗?
谢谢你的建议。
我认为这是一个愚蠢的问题,哈哈
我有以下数组
[['a','b','c'],['d','e','f']]
并希望该数组是
['a','b','c'],['d','e','f']
这意味着我想删除第一个括号。
那有意义吗?
谢谢你的建议。
这没有意义。你的意思是字符串操作?
irb(main):001:0> s = "[['a','b','c'],['d','e','f']]"
=> "[['a','b','c'],['d','e','f']]"
irb(main):002:0> s[1...-1]
=> "['a','b','c'],['d','e','f']"
或者,你想展平一个数组吗?
irb(main):003:0> [['a','b','c'],['d','e','f']].flatten
=> ["a", "b", "c", "d", "e", "f"]
不,这真的没有意义,因为 ['a','b','c'],['d','e','f'] 在这个符号中是两个独立的对象/数组,不在任何内部其他数据结构...
你可以做一个作业,比如:
a,b = [['a','b','c'],['d','e','f']]
进而
> a
=> ["a", "b", "c"]
> b
=> ["d", "e", "f"]
或者最好只遍历外部数组(因为你不知道它有多少元素):
input = [['a','b','c'],['d','e','f']]
input.each do |x|
puts "element #{x.inspect}"
end
=>
element ["a", "b", "c"]
element ["d", "e", "f"]