Its time to Write Methods
Have you ever wondered, When you click a button on a website, it takes you to another page .. How does it happen?
It happens because a programmer have written a method behind that button.. which handles everything for you ..
lets start by writing a very simple method
def display_name
puts "Bilal Haider"
end
Here is How you create a Method.
method starts with the word "def" which stands for definition and ends with the word "end". After the keyword "def", we define the name of the method, we are free to choose a name for our method. it can be any name except for they keywords. "if, while, end, for" keywords cannot be used as method name.
In between the method name, and the keyword "end" .. is the body of our method.. we can write all the instructions which we want our method to perform.
def go_to_mars
puts "I am going to MARS"
end
once the method is written. it can then be called, anywhere in the program..
Methods can be called more than once .. Whenever they are called, they will execute all the ruby instructions written within their body.
Here is example executed
## Here is How we define method
def display_name
puts "Bilal Haider"
end
## Here is our we call the method
display_name
## we can call the method any where in our program..
display_name
## Boolean Variable
display_my_name = true
## We can call methods based upon conditions
if display_my_name == true
puts "Oh I am allowed to display my name: "
display_name
end
Here is what happens when we execute our program.
Note:
In above example we defined our method once.. and we can call it multiple times.. This can come very handy .. when we want to re use a piece of code multiple times in our programs.
Here is another Example..
## Here is How we define method
def display_name
puts "Bilal Haider"
end
def display_properties
puts "Cute"
puts "Lovely"
puts "Friendly"
puts "Programmer"
puts "Can't Stop Talking about Bilal"
puts "Once you meet, you fell in love"
end
## Boolean Variable
display_my_name = true
display_my_properties = true
## We can call methods based upon conditions
if display_my_name == true
puts "Oh I am allowed to display my name: "
display_name
end
## We Check whether we are allowed to share Bilal's properties
if display_my_properties == true
puts "Bilal Has Amazing properties: "
display_properties
end
Note, that you can define more than one methods in your ruby program, and use them whenever you want.
In the above examples. I only used puts instruction in my Methods .. but you are free to use any ruby instruction whatsoever ..
You can Write a method which sends Lovely messages to everyone on internet, if you want to send a message to everyone on Steemit, about what you want to do for them .. you can do so by writing a method in ruby .. yet you have endless ideas of what you want to do with ruby methods ...
We will take more method examples in up coming article.. for now. you can create some methods of your own.
and probably share in comments.
good post @bilal-haider its usefull for programmer
Thanks for comment :) I hope people who are learning ruby . they will like my posts.
Godd postt @bilal-haider
Don’t forget her votes later in the votes back each other help
I would give you vote up. but my vote is not worth much.
Not anything
Not anything
👍👍
:)
Nice Post
Thanks
thank you for following me
You are welcome :) I will check your posts.
This post has received gratitude of 2.88% from @appreciator courtesy of @bilal-haider!