From: "Jesús Gabriel y Galán" Date: 2010-01-22T22:13:18+09:00 Subject: Re: Sorting Method On Fri, Jan 22, 2010 at 1:25 PM, Tim Pollard wrote: > I guess the real root of my problem is my inability to understand > methods and parameters. Hi, A method is a "named piece of code" you can invoke from your program. For example: irb(main):055:0> def a_method irb(main):056:1> puts "hi" irb(main):057:1> end => nil irb(main):058:0> a_method hi A method can receive parameters, which are objects bound to local variables of the method. Inside the method you can use those variables. For example: irb(main):059:0> def a_method(message) irb(main):060:1> puts message irb(main):061:1> end => nil irb(main):062:0> a_method("hi, there") hi, there There, the message variable is bound to the string you pass the method when you call it. A recursive method is a method that calls itself. You need a way to stop that, because if not you will end up with an infinite stack of calls. For example: irb(main):063:0> def a_method(number) irb(main):064:1> if number < 1 irb(main):065:2> puts "finished with #{number}" irb(main):066:2> else irb(main):067:2* puts number irb(main):068:2> a_method(number - 1) irb(main):069:2> end irb(main):070:1> end => nil irb(main):071:0> a_method(5) 5 4 3 2 1 finished with 0 The "if number < 1" is the end condition. Note that when we call a_method(number - 1) we are calling the method again, passing a different parameter. So the first time we call a_method from the outside, number is 5. Then we print it, and call a_method with 4 as the parameter, so the method executes again with number being 4, and so on until we call it with number being 0, which triggers the if condition, stopping the recursion. A more complex example would involve using the return value of the method. For example: irb(main):072:0> def factorial n irb(main):073:1> return 1 if (n == 0 || n == 1) irb(main):074:1> return n * factorial(n-1) irb(main):075:1> end => nil irb(main):076:0> factorial 5 => 120 Here the stop condition is when n is 0 or 1, for which the value of the factorial is 1. For any other value, we multiply the number by the factorial of the previous number. In order for you to better understand the call flow, let's add some print statements: irb(main):077:0> def factorial n irb(main):078:1> puts "factorial called with #{n}" irb(main):079:1> result = 0 irb(main):080:1> if (n == 0 || n == 1) irb(main):081:2> result = 1 irb(main):082:2> else irb(main):083:2* result = n * factorial(n-1) irb(main):084:2> puts "factorial of #{n} is #{result}" irb(main):085:2> result irb(main):086:2> end irb(main):087:1> end => nil irb(main):088:0> factorial 5 factorial called with 5 factorial called with 4 factorial called with 3 factorial called with 2 factorial called with 1 factorial of 2 is 2 factorial of 3 is 6 factorial of 4 is 24 factorial of 5 is 120 => 120 As you can see, the calls to factorial chain one on top of another until we reach the stop condition, then the first result is returned (1) and the next result can be calculated (2*1 = 2), and returned, then the next and so on. This doesn't shed any direct light to your sorting problem, but I hope it can clear up a little bit recursion and method calls for you. Jesus.