# Overiding method in String

**URL:** <https://www.ruby-forum.com/t/overiding-method-in-string/63703>\
**Category:** Ruby\
**Created:** [June 17, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/overiding-method-in-string/63703 "2006-06-17T21:50:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [June 17, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/overiding-method-in-string/63703/1 "2006-06-17T21:50:42Z")

</div>

Do I do something wrong here or I cannot override method upcase in  
String?

class String  
def upcase  
super.upcase  
end  
end

“abc”.upcase

Thanx for any help,  
Bojan M.

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [June 17, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/overiding-method-in-string/63703/2 "2006-06-17T22:06:02Z")

</div>

On 17/06/06, Bojan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Do I do something wrong here or I cannot override method upcase in String?
> 
> class String  
> def upcase  
> super.upcase  
> end  
> end

super is used on its own in a subclass, and indicates the superclass’s  
method of the same name:

class MyString \< String  
def upcase  
super + ‘!11’  
end  
end

s = MyString.new(‘aol’)  
s.upcase # =\> ‘AOL!11’

To modify the original class, use alias\_method:

class String  
alias\_method :original\_upcase, :upcase  
def upcase  
original\_upcase + ‘!ELEVEN’  
end  
end

s = “myspace”  
s.upcase # =\> “MYSPACE!ELEVEN”

I hope that clears it up a bit.

Paul.

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [June 17, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/overiding-method-in-string/63703/3 "2006-06-17T22:09:12Z")

</div>

On Jun 17, 2006, at 20:48, Bojan M. wrote:

> Do I do something wrong here or I cannot override method upcase in  
> String?
> 
> class String  
> def upcase  
> super.upcase  
> end  
> end
> 
> “abc”.upcase

That’s not overriding the method, but redefining it. Overriding  
takes place in subclasses, and you can definitely override the method  
(below). You’ll also note that super doesn’t work quite the same way  
as it does in Java - it’s not a reference to the superclass, it calls  
the overridden method directly; also, the superclass of String is  
Object, which doesn’t have an upcase method, so you would have had  
problems there as well.

class StringLikeThing \< String  
def upcase  
super  
end  
end

str = StringLikeThing.new(“foo”)  
=\> “foo”  
str.upcase  
=\> “FOO”

You can also redefine the method, if that really is what you’re after.

class String  
def upcase  
“This used to be the upcase method, now it’s gone!”  
end  
end  
“abc”.upcase  
=\> “This used to be the upcase method, now it’s gone!”

If you still want to use the old behaviour, but with some  
modification, you can use an alias:

class String  
alias :real\_upcase :upcase  
def upcase  
"upcase: " + real\_upcase  
end  
end  
“abc”.upcase  
=\> “upcase: ABC”

Hope that helps.  
matthew smillie

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [June 17, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/overiding-method-in-string/63703/4 "2006-06-17T22:12:19Z")

</div>

Matthew S. wrote:

> > 
> 
> class StringLikeThing \< String  
> You can also redefine the method, if that really is what you’re after.  
> you can use an alias:  
> Hope that helps.  
> matthew smillie

Thanks for both quick answers, it’s all clear now. I used aliasing to  
redefine module methods.

best regards,  
Bojan M.
