2016-01-30 98 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對象返回一個字符串與單元格。仍然無法打印它... –