Question about return in method using Lesson 19


#1

Link: Ruby. Methods, Blocks, & Sorting Lesson 19

So I have been putting some code in lesson 19 on "Methods, Blocks, & Sorting." I put some code to verbose the progress so I could understand how does 'sort' method work. First, I thought whatever code that goes inside 'sort' method, this method only receives "-1, 0, or 1" for each of its iteration. With that in mind, I made a function (I called it sort__value__desc)to print the progress. This worked, I could see the progress. But then to add clarity, I 'upcase'-ed both item that were sent to sort__value__desc. In this function I only return an integer for every if. The question is: Why does the sorted strings are upcased too? Shouldn't those upcased strings are only in the function?

def sort_value_desc (a, b)
    a.upcase!
    b.upcase!
    if a > b
        puts "Comparing #{a} to #{b}. Result: -1"
        return -1
    elsif a < b
        puts "Comparing #{a} to #{b}. Result: 1"
        return 1
    else
        puts "Comparing #{a} to #{b}. Result: 0"
        return 0
    end
end

fruits = ["orange", "apple", "banana", "pear", "grapes"]
itr = 1
fruits.sort! do |fruit1, fruit2|
    puts "Iteration #{itr}"
    itr += 1
    sort_value_desc(fruit1, fruit2)
end

And the results (see the last line in red to see what I mean):

Iteration 1
Comparing ORANGE to BANANA. Result: -1
Iteration 2
Comparing BANANA to GRAPES. Result: 1
Iteration 3
Comparing ORANGE to GRAPES. Result: -1
Iteration 4
Comparing APPLE to GRAPES. Result: 1
Iteration 5
Comparing PEAR to GRAPES. Result: -1
Iteration 6
Comparing APPLE to BANANA. Result: 1
Iteration 7
Comparing ORANGE to PEAR. Result: 1
["PEAR", "ORANGE", "GRAPES", "BANANA", "APPLE"]

#2

You are modifying a and b, you could create up-cased copies instead for the sake of comparing
Oh and I do think that should be considered reverse order.. but then again if that's what you wanted then it makes sense to invert the comparing code


#3

Isn't 'a' and 'b' are local variables that shouldn't be sent back to the method/function that call it? Because I didn't return 'a' nor 'b'. Hmm, or ruby treats variables different then other programming languages (like c#)?


#4

Yes and no.

They are local, but the values are references to strings, same strings as are in the list

(references are more like pointers than c# references which mean more like "same variable")

I think passing a list (an object) to a function in c# would be the same thing, I'll see if I can manage a few c# lines as an example


#5

using System.Collections.Generic;

public class Example {
    public static void Main() {
        List<int> list = new List<int>();
        f(list);
        foreach (int num in list) {
            System.Console.WriteLine(num);  // prints 5
        }
    }

    static void f(List<int> list) {
        list.Add(5);
    }
}

#6

Thanks a lot ionatan. This is my first time returning to programming since years but, isn't that because you made a 'static' method? Btw thanks a lot for doing this :slight_smile:


#7

Static just means that it can be called on its own, without the context of an object

For example, List.Add is not static - it only makes sense to call in the context of a List object

The Main method has to be static. I could have instantiated Example and then called f on that instance, but the f method wouldn't be using that object anyway, so it may as well be static


#8

Gotcha, thanks a lot!


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.