2016-01-30 75 views
0

我在努力打印一个对象。 我来自Python,所以Ruby的OO方面并不那么难,但有些机制很难学。 从来没有关于通过覆盖python中的to_s方法打印对象的问题,但在那里,我不能。我让你看下面的代码。 (我也想保持我的项目的队友这个小小的学生项目的RDoc)无法打印对象

gem "rmagick" 
gem "rdoc" 
require "rmagick" 
require "rdoc/rdoc" 
include Magick 
## 
# This class represents the complete +Game+ 
class Game 
    # Debugging accessor 
    attr_accessor :engine 

    ## 
    # Creates a new instance of the game 
    def initialize() 
     @engine = Engine.new 
    end 

    ## 
    # Starts the engine 
    def start() 
     @engine.genRandomGrid() 
    end 
end 
## 
# This class represents the +Controller+ from +MVC+ 
class Engine 
    # Debugging accessor 
    attr_accessor :grid 

    ## 
    # Initiates the needed instance's variables 
    def initialize(x = 10) 
     @grid = Grid.new(x) 
    end 

    ## 
    # Generates a x * x grid of +Cell+ with false value/state 
    def genNewGrid(x = 10) 
     @grid = Grid.new(x) 
    end 

    ## 
    # Fills the current grid with random value 
    def genRandomGrid() 
     @grid.randomGrid() 
     p @grid 
    end 

    ## 
    # Fills the current grid with picture's values 
    def genPictureGrid(path) 
     @grid.picture(path) 
    end 
end 

## 
# This class represents a +Grid+ 
class Grid 
    # Debugging accessor 
    attr_accessor :maxLen #Longueur/Largeur 
    # Debugging accessor 
    attr_accessor :matrix #2 listes. Voir object Cell 
    # Debugging accessor 
    attr_accessor :xIndices #Les indices au dessus des abscisses 
    # Debugging accessor 
    attr_accessor :yIndices #Les indices à gauche des ordonnées 

    ## 
    # Initializes [email protected]+, [email protected]+ and [email protected]+ with 2D Array 
    def initialize(x = 10) 
     @maxLen = x 
     @matrix = Array.new(x){Array.new(x){Cell.new(false, false)}} 
     @xIndices = Array.new(x){Array.new} 
     @yIndices = Array.new(x){Array.new} 
    end 

    ## 
    # Turns to false a true cell state and to true a false cell state 
    def changeCellState(x, y) 
     @matrix[x][y].changeState 
    end 

    ## 
    # Returns the @maxLen if needed 
    def getLength() 
     return @maxLen 
    end 

    ## 
    # Generates "randomly" the value of each grid's cell 
    def randomGrid() 
     @matrix.each do |j| 
      j.each do |x| 
       rand_value = Random.srand(Random.new_seed) 
       if (rand_value % 2) == 0 
        x.setValue(true) 
       else 
        x.setValue(false) 
       end 
      end 
     end 
     evalIndices() 
     # NB : Class is array 
     p #{@matrix} 
    end 

    ## 
    # TODO : Generates a grid from a picture 
    def picture() 

    end 

    ## 
    # Fills [email protected]+ and [email protected]+ with right values 
    def evalIndices() 
     _row = 0 
     _in = false 
     _nb = 0 
     @matrix.each do |j| 
      _in = false 
      _nb = 0 
      j.each do |x| 
       if x.getValue 
        _in = true 
        _nb += 1 
       elsif _in 
        @xIndices[_row].push(_nb) 
        _in = false 
        _nb = 0 
       end 
      end 
      if _in 
       @xIndices[_row].push(_nb) 
       _in = false 
       _nb = 0 
      end 
      _row += 1 
     end 

     for j in [email protected] 
      _in = false 
      _nb = 0 
      for i in [email protected] 
       if @matrix[i][j].getValue 
        _in = true 
        _nb += 1 
       elsif _in 
        @yIndices[j].push(_nb) 
        _in = false 
        _nb = 0 
       end 
      end 
      if _in 
       @yIndices[j].push(_nb) 
       _in = false 
       _nb = 0 
      end 
     end 
    end 

    def to_s 
     ret = "" 
     @matrix.each do |j| 
      j.each do |cell| 
       ret += cell.getValue 
      end 
      ret += "\n" 
     end 
     return ret 
    end 
end 


## 
# This class represents a +Cell+ 
class Cell 
    # Debugging accessor 
    attr_accessor :state #Etat graphique (GUI) 
    # Debugging accessor 
    attr_accessor :value #Valeur réelle (MOTEUR) 

    ## 
    # Initializes the [email protected]+ and [email protected]+ with parameters 
    def initialize(state, value) 
     @state = state 
     @value = value 
    end 

    ## 
    # Makes the +Cell+ object printable 
    def to_s 
     if @value 
      "[X]" 
     else 
      "[ ]" 
     end 
    end 

    ## 
    # Boolean access method 
    def changeState() 
     if @state == true 
      @state = false 
     else 
      @state = true 
     end 
    end 

    ## 
    # Access method 
    def setValue(value) 
     @value = value 
    end 

    ## 
    # Access method 
    def getState() 
     return @state 
    end 

    ## 
    # Access method 
    def getValue() 
     return @value 
    end 

    ## 
    # Verifies if [email protected]+ and [email protected]+ are the same 
    def right? 
     return @state && @value 
    end 
end 


game = Game.new 
game.start() 

在引擎类我想要在genRandGrid方法打印@grid但只打印0 .. 9。

Thx。

+0

你尝试过'p'吗? –

+0

p @ grid给了我相同的结果。我有一些奇怪的东西。有时我有对象和内容的“指针”,其他时间是0..9范围,其他时间错误。我今天真的开始了ruby,所以如果我不能在Python中轻松完成某些任务,这有​​点令人沮丧。我在对象中创建对象,也许我以Pythonic的方式做到这一点,这种方式不能用Rubyonic的方式完成。 –

+0

只是看看你的代码,看到一些'for'循环!在Ruby中,如果可以的话,你应该使用'each'或它的一个衍生物:) –

回答

0

当您打印它时,@grid变量的类型是什么?

事实证明,它是由@grid.random返回的类型,它不是Grid对象。您可以打印类为保证,并修复代码:

puts @grid.class 

代码您共享的需求相当一些改进。 Ruby是一种非常宽容的语言,现在,代码无法很好地管理类型。另外,我不认为习惯用Ruby来实现你的目标是必要的,但是代码可以被简化很多,这可以简化对几个方面的推理,包括类型。

+0

我试图将evalIndices方法转换为Ruby的way_to_do,但是如何通过x先读取y的数组,然后再通过x?最后,我只想得到一个包含对象的可打印矩阵(我的Cell对象可打印)。 @ grid.class的类型是'Array'。 –

+0

如何?我只是明白你为什么这么告诉我。我有一个数组而不是网格对象...我怎样才能返回对象,而不是数组的数组?我不能相信这是错误的...初始化方法不会返回一个数组,但没有值的权利? (像Python中的NoneObject?)。我对这种语言感到惊讶和无聊^^。我不知道如何解决这个问题,我开始使用这种语言几个小时... –

+0

我编辑了代码,感谢您的帮助。有了这个我仍然无法打印我也有to_s方法在两个对象(#Cell对象返回[X]或[]如果@值是真或假)和#Grid对象返回一个字符串与单元格。仍然无法打印它... –