Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 20th, 2011, 02:54 AM
Giac0m0 Giac0m0 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 1 Giac0m0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 51 sec
Reputation Power: 0
Ruby - Recursive stack level too deep

Hi there,

I'm trying to run the following script:
PHP Code:
class Node
  
  data 
nil
  children 
nil
  
  attr_accessor 
:children, :data
    
  def initialize
(datachildren=Array.new)
    
self.data data
    self
.children children
  end
  
  def addchild
(child)
    
self.children << child
  end
  
  def printtree
()
    print 
self.data.url"\n"
    
self.children.each{ |child
      print 
child.printtree()
      }
  
end  
end 

But the method printtree is running till the stack level is too deep.... this is strange because it should not. So i'm sure I made a type in my ruby script. But i'm pretty new to ruby so obviously i don't see it haha.

Hope you can help me. That would be a blast!

Thanks,
Giac0m0

Reply With Quote
  #2  
Old November 17th, 2011, 05:10 PM
twostepted's Avatar
twostepted twostepted is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2004
Location: Central Washington (USA)
Posts: 518 twostepted User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 30 m 12 sec
Reputation Power: 10
Send a message via Yahoo to twostepted
This runs fine for me, printing the parent nodes data first and then the children. Maybe you're passing the children incorrectly to Node.new()?

Code:
class Node 
   
  #data = nil 
  #children = nil 
   
  attr_accessor :children, :data 
     
  def initialize(data, children=Array.new) 
    self.data = data 
    self.children = children
  end 
   
  def addchild(child) 
    self.children << child 
  end 
   
  def printtree() 
    print self.data, "\n" 
    self.children.each{ |child|  
      print child.printtree() 
      } 
  end   
end

n1 = Node.new('n1')
n2 = Node.new('n2')
n3 = Node.new('n3', [n2, n1])

#n2.printtree
n3.printtree

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Ruby - Recursive stack level too deep

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap