Today in this tutorial we will learn how to Write different methods and use of conditional statements in Ruby.
- Follow the steps below and get the hands on experience :
- To start with ruby compiler go to
Start->All Programs->RailsInstaller->Interactive Ruby
- After Opening the Interactive Ruby the following Command line interface will be displayed.
- To declare the method in ruby the syntax is as shown below :
- For Method With Arguments :
- System defined methods can also be declared as follows
- If you want to know the list of all the methods in the ruby which may be working with string and numbers then use the following syntax :
- Now here is the exercise for Conditional Statements :
- if..else is one of the conditional statement
- We can also put ‘&&’ and ‘||’ operator in it
- Thus we have learnt how to write methods and conditional statements in Ruby on Rails.
def methodname body of methods end
So in this if we have to create method the def keyword is used and to finish method end keyword is used.
def methodname(arg1,agr2,arg3…,argn) methodbody methodbody end
The reverse function is as follows:
In this example we have taken variable fruit1 =”mango”
we have taken one more variable fruit2 =”apple”
Now if we compare it mango or apple it should display “This is a fruit” .
if(fruit1=="mango") print 'This is fruit' end
if(fruit1=="melon") print 'This is not mango' end
fruit1="Mango" fruit2="Grapes" if(fruit1=="Mango"&&fruit2=="Grapes") puts"I love both fruits" end