21 Mar 2013, 22:29
Distorted_pragsmall

Enrique Sánchez (17 posts)

I’m having trouble understanding the solution of the Rite of Passage: Sorting exercise.

For example the following code:

def sort arr
  rec_sort arr, []
end

</code>

Why is there a comma between arr and []? I haven’t seen this type of formatting before in the book…

Or when defining the rec_sort method:

def rec_sort unsorted, sorted

Where are we defining unsorted and unsorted as arrays? How does Ruby know they are arrays?

I’m just getting very confused by these little details…

25 Mar 2013, 17:27
Med__2008.08.19.09.44.43_pragsmall

Chris Pine (59 posts)

Ruby (and most languages) use a comma-separated list to pass multiple values to a method. If this were not Ruby, it was probably look more like

rec_sort(arr, [])

Where are we defining unsorted and unsorted as arrays?

We aren’t. Variables in Ruby are not typed.

How does Ruby know they are arrays?

Ruby knows they are arrays when you pass in arrays (and not a moment before). @:)@

17 Apr 2013, 23:56
Distorted_pragsmall

Enrique Sánchez (17 posts)

Hi Chris,

What happened is that I was so exited and eager to learn programming that didn’t think that I have to take breaks from time to time, otherwise my brain will suffer from overload.

Took a break, came back to the code and everything was clearer.

Thank you for your help :)

18 Apr 2013, 16:39
Med__2008.08.19.09.44.43_pragsmall

Chris Pine (59 posts)

Awesome! Yes, breaks are important. :-)

  You must be logged in to comment