Sorting with methods


whats the problem in this code when I enter it it comes up with: undefined method `alphabetize!' for [1, 2, 3, 4, 5]:Array
here is the code:
def alphabetize(arr, rev = false )
numbers = [1,2,3,4,5]
puts numbers


By calling numbers.alphabetize!, you're saying that an object of type 'array' responds to a method named alphabetize!. Unless you're extremely lucky and the people which designed Ruby have given a method of that name to the array objects, this won't work.

You need to call alphabetize directly and give it an array as an argument:

method(argument1, argument2, ...)


So let me get this straight....

what they're actually asking us to do is not actually possible? By this I mean, the example code they provide is

numbers = [5, 1, 3, 8]
puts numbers

They want us to mimic that.. but call it differently to pass the challenge? That doesn't make any sense.


They want you to define a method using sort!, not a method mimicking sort!.

The example shown with numbers.sort! is used to explain what feature they want your method to have.

It's the same thing with reverse! in the next exercise.


@Coursecoder78160 - You are trying to return numbers in your example above "alphabetized" but the way Ruby interprets the code is different than you are expecting.

You would want to call a method "alphabetize" as you have defined above, similar to this:

def alphabetize(arr, rev=false)

That is a method ^^^^

Now if you wanted to call this method you need to provide an array, which is what they have done in the example with this exercise ->

numbers = [1, 2, 3, 4, 5]

If you wanted to "alphabetize" this array of "numbers" you would need to call the method against the array such as this:

puts alphabetize(numbers)

That will call the method defined above to run through your array of "numbers", 1 2 3 4 5 and sort them or "alphabetize" them.


Only one member in this thread is posting code correctly. Please, folks... Do try and learn how to post code samples so others can read them and SEE the indentation. Paramount in the Ruby forums. Thank you.