Search code examples
rubyrecursionnomethoderror

Ruby: NoMethodError: undefined method `[]' for #<Enumerator:0x007f8683922870>


EDIT TWO

I noticed that while running the program, it was also returning nil as the last item in the array. To fix this I changed my condition.

if array_list.empty? ----> if array_list.length == 1

This returns the array without nil

EDIT Thanks to @mudasobwa for his help.

He pointed out a couple things wrong with my program: one with how I wrote array.index using [ ] instead of the required ( )

Also learned that I shouldn't put spaces between the method name and its parameters.

Updated code here, look at original to see how it had changed.

list_one = ["apple", "anna", "banana", "peach", "cherry", "kiwi", "pineapple"]

def sort_array(array_list, sorted_array=[])

  sorted_array.push(array_list.min)
  if array_list.empty? || array_list.nil?
    return sorted_array
  else
    array_list.delete_at(array_list.index(array_list.min))
  return sort_array(array_list, sorted_array)
end

end

sort_array(list_one)

If anyone has other improvements or suggestions, please let me know. As it is, this recursive function seems to work flawlessly, although I still have a lot to learn on how to implement recursive functions in the future. It can be hard to get my brain wrapped around it.

END EDIT, ORIGINAL BELOW

I'm reading through Chris Pine's book Learn to Program, and one of the exercises is to make a recursive sorting method to sort through a list of items.

Here is my code:

list_one = ["apple", "anna", "banana", "peach", "cherry", "kiwi", "pineapple"]

def sort_array (array_list, sorted_array=[])
  sorted_array = sorted_array
  sorted_array.push(array_list.index[array_list.min])
  if array_list.empty?
    return sorted_array
  else
    array_list.delete_at(array_list.index[array_list.min])
    return sort_array(array_list, sorted_array)
end

end

sort_array (list_one)

Now, Chris's book calls for using a wrapper function, which I tried incorporating, but it doesn't make any difference in this case. Just coming up with this has been a bit of a mind-boggler, and I needed to read up more on recursive functions to get to this point.

When running this, I get the error listed in the title. I haven't had any luck yet figuring this out, so I'm hoping someone here can shed some light on what I might be doing wrong.

If I incorporate Chris's wrapper function:

def sortme (some_array)
  sort_array(some_array, [])
end

the error remains the same, which makes sense considering that the wrapper function is just calling the sort_array function. I really don't see the need for it in this case.

Any thoughts? Why am I getting this error?


Solution

  • The problem is here:

    sorted_array.push(array_list.index[array_list.min])
    

    Either use Array#index or Array#[]:

    sorted_array.push(array_list.index(array_list.min))
    # or sorted_array.push(array_list[array_list.min])
    

    There are two (or more) glitches with this code:

    sorted_array = sorted_array
    

    is a noop. What did you want to accomplish with that?

    And, in general, never ever put a space between a method name and an opening parenthesis starting a list of it’s parameters. It might not work as expected in some cases.