# .capitalize! not being recognized in home made sort method

**URL:** <https://www.ruby-forum.com/t/capitalize-not-being-recognized-in-home-made-sort-method/122927>\
**Category:** Ruby\
**Created:** [December 12, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/capitalize-not-being-recognized-in-home-made-sort-method/122927 "2007-12-12T21:10:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nigama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@nigama](https://www.ruby-forum.com/u/nigama)\
**Post date:** [December 12, 2007, 9:10pm UTC](https://www.ruby-forum.com/t/capitalize-not-being-recognized-in-home-made-sort-method/122927/1 "2007-12-12T21:10:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![nigama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@nigama](https://www.ruby-forum.com/u/nigama)\
**Post date:** [December 12, 2007, 9:29pm UTC](https://www.ruby-forum.com/t/capitalize-not-being-recognized-in-home-made-sort-method/122927/2 "2007-12-12T21:29:08Z")

</div>

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
