Ruby: Comments

In Ruby we can add comments to our code in two ways

  1. Single line comments
  2. Multi-line comments

For single line comments, we use # before the comment to be added.

For eg: #this is a comment

For multi-line comment; we use =begin at the start and =end at the end.

For eg:

=begin

I want to add

multi line

comment

=end

Note: There should be no space between = and begin/end

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

Ruby: Mathematics

There are 6 arithetic operators which we would discuss at this point

  1. Addition +
  2. Subtraction –
  3. Multiplication *
  4. Division /
  5. Exponentiation **
  6. Modulo %

All are self-explanatory but for the beginners: Exponentiation raises one number (the base) to the power of the other (the exponent).

For eg: 2 ** 3 is 8 [ Basically it means 2*2*2 ]

Coming to Modulus: it returns the remainder of division.

For eg: 17 % 5 would be 2