[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

.capitalize! not being recognized in home made sort method

NigamaX

12/12/2007 8:11:00 PM

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?
--
Posted via http://www.ruby-....

1 Answer

Sebastian Hungerecker

12/12/2007 8:24:00 PM

0

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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826