Ruby: Methods

Everything in Ruby is an object and has specific built-in abilities knows as “methods”.

For eg strings have built-in methods that can tell you the length of the string, reverse it, capitalize it and much more.

.length method:

It returns the length of the string.

for eg:

my_name=”Myname”.length

Ruby will output =>> 6

.reverse method

This method reverse the string that we provide.

For eg:

“Ayesha”.reverse

Ruby will output => ahseyA

.upcase & .downcase

These two methods can be used to change the case of a string.

As the name suggests, .upcase would change the entire string to uppercase and .downcase would change the entire string to the lower case.

For eg:

puts “Ayesha”.downcase would give output =>> ayesha

puts “Ayesha”.upcase would give output =>> AYESHA

Published by

Rocking Londoner

www.rockinglondoner.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s