Я так стараюсь напечатать объект. Я родом из Python, поэтому OO-аспект Ruby не так уж и трудный, но некоторые механики трудно освоить. Никогда не возникало проблем с печатью объекта путем переопределения метода to_s в python, но там я не могу. Я позволю вам посмотреть следующий код. (я также пытаюсь поддерживать 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()
В классе двигателей я пытаюсь напечатать @grid в методе genRandGrid но это только печатает 0 .. 9.
Thx.
Вы пробовали 'p'? –
p @grid дает мне тот же результат. У меня есть странные вещи. Когда-то у меня есть «указатель» объекта и содержимого, в другое время - диапазон 0..9, другие ошибки времени. Сегодня я действительно начал рубин, поэтому немного разочаровываю, что не могу делать то, что я могу легко сделать в Python. Я создаю объекты в объектах, может быть, я делаю это по-питоническому, что невозможно сделать таким образом в Rubyonic :). –
Просто взглянув на ваш код и увидел несколько циклов 'for'! В Ruby вы должны использовать 'each' или одну из своих производных, если можете :) –