在红宝石矿上运行一个红宝石项目

时间:2015-05-31 18:47:39

标签: ruby rubymine

我是ruby编程的新手。我写了一个小程序来递归计算二维数组中的blob。它包含两个类,单元格和blob。我收到以下错误,我不知道它意味着什么或如何解决它。

附件是我的代码和错误。 细胞类

class Cell

  attr_accessor :data,:visited,:row,:col


  def initialize(data, row, col)
      @data = data
      @visited = false
      @row = row
      @col = col
  end

  def to_s
    self.data.to_s
  end

end

Blob class

class Blobs
 require ./Cell
 @cells = Array.new(10){Array.new(10)}

 def separate(percentage)
   for i in 0..10
     for i in 0..10
       random = Random.rand(0,100)
       if random < percentage
         @cells[i][j] = Cell.new('x',i,j)
       else
         @cells[i][j] = Cell.new(nil, i, j)
       end
     end
   end
 end

 def markBlob(currentCell)
   if currentCell.visited
     return
   end
   currentCell.visited = true
   if currentCell.data.nil?
     return
   end

   if currentCell.row >0
     markBlob(@cells[currentCell.row-1][currentCell.col])
   end

   if currentCell.row <@cells.size-1
     markBlob(@cells[currentCell.row+1][currentCell.col])
   end

   if currentCell.col>0
     markBlob(@cells[currentCell.row][currentCell.col-1])
   end

   if currentCell.col<@cells.size-1
     markBlob(@cells[currentCell.row][currentCell.col+1])
   end

 end

 def countblobs
   count = 0
   for i in 0..10
     for j in 0..10
       cell = @cells[i][j]
       if !cell.visited && cell.data.nil?
         count++
         markBlob(cell)
       end
     end
   end
   return count
 end

 def to_s
   for i in 0..10
     for j in 0..10
       if @cells[i][j].data.nil?
         puts '- '
       else
       puts @cells[i][j].data + ' '
         end
     end
     puts "\n"
   end

 end

 blob = Blobs.new
 number = blob
 puts number

  end

这是我得到的错误:

C:\Ruby22\bin\ruby.exe -e         $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)         C:/Users/Nechama/RubymineProjects/blobs/blobs.rb
C:/Ruby22/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:38:in   `require': wrong number of arguments (0 for 1) (ArgumentError)
    from C:/Users/Nechama/RubymineProjects/blobs/blobs.rb:3:in `<class:Blobs>'
    from C:/Users/Nechama/RubymineProjects/blobs/blobs.rb:2:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

Process finished with exit code 1

1 个答案:

答案 0 :(得分:0)

require应该将 string 作为参数。

使用require_relative到单元格文件

require_relative 'cell'

并将其置于class Blobs.

之上

有关Including Other Files In Ruby

的更多信息