.capitalize! not being recognized in home made sort method

def sort some_array
some_array.each do |n| # this code seems to be ignored
n.capitalize! #
end #
recursive_sort some_array, []
end

def recursive_sort unsorted_array, sorted_array
unsorted_array.each do |n|
if n < unsorted_array[n…((unsorted_array).length)]
sorted_array << n
end
end
end

a = [“beaver”, “Cat”]
puts a.sort

The program will sort fine, by the uppercase A-Z first, followed by
lowercase a-z, but it seems to be ignoring the capitalization.
Strangely, it does work in irb…

a = [“aa”, “bb”, “cc”]
a.each do |n|
n.capitalize!
end

Returns: [“Aa”, “Bb”, “Cc”]

So it works fine in irb, but not from within my method.
I believe it’s in the right place, as the intention is to capitalize!
the array before the recursive_sort takes place. I’ve moved it around
to see if it might work elsewhere in the two defined methods but got
nada. Looked through Google and docs, but oddly everything says it
should work.

Any thoughts?

Nigama Xx wrote:

def sort some_array

puts a.sort

You’re defining Object#sort(some_array), but you’re calling
Enumerable#sort().
You’d call your method by writing sort(a) not a.sort. If you’d want to
call it
via a.my_sort, you’d have to define it like

module Enumerable # or class Array
def sort

end
end

HTH,
Sebastian

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs