.capitalize! not being recognized in home made sort method

N

Nigama Xx

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?
 
S

Sebastian Hungerecker

Nigama said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top