Extending Core Ruby Classes
Add a Custom Method to the String Class
1
Open IRB
Type 'irb' in Terminal to start Interactive Ruby.
2
Reopen the String Class
class String; def reverse_caps; self.reverse.upcase; end; end
3
Use 'self' to Reference the Instance
Inside the method, self is the current string value.
4
Call Your New Method
s = "Just a string"; s.reverse_caps returns GNIRTS A TSUJ.
Build Web Development Skills at Noble Desktop
Noble Desktop's Full-Stack Web Development Certificate teaches modern web development concepts that transfer across Ruby, Python, and JavaScript stacks.
Learn how to extend core Ruby classes and create new methods in this comprehensive Ruby on Rails tutorial.