Ruby Programming Tutorial - Lesson 15 - Writing Methods in Ruby
ruby·@bilal-haider·
0.000 HBDRuby Programming Tutorial - Lesson 15 - Writing Methods in Ruby
 ## 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.
👍 bilal-haider, xaunya, threadripper, jdcrunchman, mohdiqbal07, steinhammer, borrov, petervroom, muliadi52, reggie031, eviarahi, shizuka, gvab, oscurity, andi07, saifulsl, syahrulsteem11, essendi, sinilga, suheri386, jedagonza, lauramcpeak, sakanachan, n0man, yuneidi, popcha, jidaropaymas, goldhead, jawker, yerd, cris.lieva, issuestock, laurashsat, yorliare, randowhale, appreciator, upmyvote, upme, arabbymajumder, rmasoud, yudithvalero, androidmaster, proctologic, derpdragon,