Ruby 三维阵列

Ruby 三维阵列,ruby,arrays,Ruby,Arrays,我有以下代码: require 'chunky_png' time1 = Time.now image = ChunkyPNG::Image::from_file("logo10.png") height = image.height width = image.width nouvelle = Array.new height.times do |x| nouvelle[x] = Array.new width.times do |y| nouvelle[x][y] = A

我有以下代码:

require 'chunky_png'

time1 = Time.now
image = ChunkyPNG::Image::from_file("logo10.png")
height = image.height
width = image.width
nouvelle = Array.new

height.times do |x|
  nouvelle[x] = Array.new
  width.times do |y|
    nouvelle[x][y] = Array.new
    nouvelle[x][y][0] = ChunkyPNG::Color.b(image[x,y])
    nouvelle[x][y][1] = ChunkyPNG::Color.g(image[x,y])
    nouvelle[x][y][2] = ChunkyPNG::Color.r(image[x,y])
  end
end 

time2 = Time.now
puts "temps = " + ((time2 - time1)*1000).to_s + " ms"
我有一个问题:

syntax error, unexpected tIDENTIFIER, expecting keyword_end
        nouvelle[x][y] = Array.new
                        ^
syntax error, unexpected tIDENTIFIER, expecting keyword_end
        nouvelle[x][y][2] = ChunkyPNG::Color.r(image[x,y])
                           ^
我看不出问题出在哪里。你能帮我吗?

事实上,我的代码中有一个红点,你可以通过这个链接查看


nouvelle[x][y]=-->在]和等号之间有一个字符未知

以下是解决此类问题的方法:

# encoding: UTF-8

str = '] = A'
str.chars.map(&:ord).join(', ')
# => "93, 160, 61, 32, 65"
请注意,第二个值是160,位于第二个字符处。160十进制是0xA0十六进制或Unicode中的NBSP

然后我输入了字符串:

str = '] = A'
str.chars.map(&:ord).join(', ')
# => "93, 32, 61, 32, 65"

再次注意,第二个字符现在是32,也就是0x20,也就是“空格”。

你确定问题在这里吗?是的,因为这都是代码。请进行编辑,说明问题已确定。在看到你的答案之前,我考虑了你的问题好几分钟。