small medium large xlarge

21 Mar 2013, 22:29
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, []


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
Chris Pine (72 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
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
Chris Pine (72 posts)

Awesome! Yes, breaks are important. :-)

You must be logged in to comment