Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Basics Ruby Syntax Method Arguments

Hi! I get "stack level too deep" when I try to push def say(whatever) puts say("Ruby") whatever = "Ruby" end

I keep finding conflicting accounts of what this means online.

say.rb
def say(whatever) 
  puts say("Ruby")
  whatever = "Ruby"
end

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, sarah haddon! There's something called "recursion" going on here. Recursion happens when a function/method calls itself. To call a function/method you just need to give the name of the method and any arguments it needs.

On this line:

 puts say("Ruby")

That calls the method say(), but it's calling it from within the say() method. You have this scenario going on where you call the method from within the method which then calls itself again out into infinity. It's a little like standing between two mirrors that face each other and you can see infinite mirrors. Because the mirror is reflecting the mirror that is reflecting the mirror... etc.

This is what is meant by "max recursion depth". You've implemented something that is not really an infinite loop, but you have something that is calling itself and repeating out into infinity. Recursion can be useful in a handful of cases, but only when you can guarantee that it will stop at some point and before it eats up all of your memory or reaches the maximum recursion depth.

What you're looking for is something much simpler:

def say(whatever) # this is the name of the method and it takes a string 
  puts whatever  # this prints out whatever we send
end

say("Ruby") # call the method and send it the string "Ruby"

Hope this helps! :sparkles: