Hi,
I'm trying to learn ruby by 'borrowing' peoples code and adding comments
to help me understand what they've done. The code below is 'borrowed' from
the maze solutions on rubygarden.
(http://wiki.rubygarden.org/Ruby/page/show/NubyMazeSolutions)
I'm trying to understand the line:
@paths = Array.new(0,Array.new(0))
From 'ri Array.new' I think the outer Array is of the form:
Array.new(size=0, obj=nil)
where obj=nil is Array.new(0) ???
If so could someone explain whats going on here? I can understand
something like Array.new(5,"A") where a new Array with 5 references to the
same string "A" is created. But why create an Array with 0 elements and
have 0 references to another new empty array? How can you have 0
references?
Aren't these equivalent?
irb(main):034:0> Array.new(0,Array.new(0))
=> []
irb(main):035:0> Array.new(0)
=> []
irb(main):036:0> Array.new
=> []
I can replace that line in the code below with any of the 2 commented
lines below it and the program still runs. So I'm wondering why the author
used that particular line. I understand no one can speak for the author
but am querying whether I'm missing something here.
···
--------------------------------------------------------------------------
class MazeSolver
# the constructor with the maze string as a parameter
def initialize(m)
# assign the maze to the instance variable @maze
# (only visible to a particular instance of the class
# as opposed to a class variable (@@) which is accessible
# to *any* instance of the class)
@maze = m
# find the length of the first line. ie count chars to first \n
# (indexing starts at 0 so + 1)
@rowlen = @maze.index("\n")+1
# create an array of size 0, ??
# with 0 references to another new empty array ???
# hah?
@paths = Array.new(0,Array.new(0)) <<<<<<<<<<<<
# @paths = Array.new
# @paths = Array.new(0)
# call the findPaths method
findPaths
end
#
def getNSEW(index)
# create an empty array
ret=[]
#
table =[index-@rowlen,index+@rowlen,index+1,index-1]
table.each{|n|ret.push([n,@maze[n].chr])}
return ret
end
def findPaths(loc=@maze.index('s'),current=[])
sides = getNSEW(loc)
return 0 if current.include?(loc)
current.push(loc)
ret=0
(0..3).each do |n|
case (sides[n][1])
when "e": current.push(sides[n][0]);@paths.push(current);return 1
when "-": ret += findPaths(sides[n][0],current.dup)
end
n+=1
end
ret
end
def to_s
ret=""
@paths.each do |path|
tMaze = @maze.dup
path.each{|loc|tMaze[loc]="*"}
ret += "#{tMaze.gsub(/#/,"I").gsub(/-/," ")}\n\n\n"
end
ret
end
end
# a heredoc created string representing the maze
mazeDef = <<MAZE_STRING
#########################
#s--###----#####-#####-##
###-#---##-##--#-##-----#
###-#-#-#--##-##-##-###-#
#---#-#-#-###-##-##-##--#
#-#---#-#---#-##----##-##
#-#-#-#-#-#-#-##-########
#-#-#-#-#-#---##--------#
#####-###---###########-#
#####-#####-###########-#
#####------------------e#
#########################
MAZE_STRING
# 'print' calls the to_s method of a newly created MazeSolver
# instance where mazedef is passed to the initialize method.
print MazeSolver.new(mazeDef)
--------------------------------------------------------------------------
thanks,
--
Mark