Learn Ruby: Method
Hello friends, from this period of time. I’ll try my best to write more article that all of us could learn together and I hope you like that idea. Today, I’m going to talk about a programming language that makes programmers be happy, be productive, and enjoy coding – this is Ruby. More specifically, we’re orient method in Ruby programming language. See what syntax it is, how it works, examples and so on…
Like other programming languages, methods in Ruby work like a function, which are used to bundle or more repeatable statements into a single unit.
Define a method without parameter(s).
def method_name puts "Hello Ruby!" end
As you can see the syntax above, a method in Ruby starts with
Define a method with parameters.
def myMethod(x = 50, y = 30) result = x + y puts result end myMethod myMethod(20, 10) # myMethod outputs: 80 # myMethod outputs: 30
As you can observe, a method above has been defined with two parameters x and y, inside the method we defined a variable that sums the two numbers x, y and finally, you have to call a method to execute the program.
But wait, why I call method myMethod without passing require parameters and it’s still working? Because we’ve just defined something calls default values. So in that way, if you had defined the default values which were 50 and 30 in the example, you didn’t pass any require parameters and it still gives you the result. In that case, it’s 80 by default. Nevertheless, the method that I called the second time it has the passing require values, so the method should be returned by what parameters had been passing in. Remember if you don’t want to define the default values, you should pass them when you call a method to avoid errors.😅And one more thing, the number of parameters when you define a method should be equal the number of values passing in when you call the method. For example, if a method
Return values from methods
So what is value it will return? x,y or z or all of them? The answer
We’ve just listed return values beneath, what about the return statement? A lot of people skeptical about
def multiply(a,b) product = a * b return product if product > 9 puts "Bigger than 9" else puts "Small than 9" end end puts multiply(2,5) # Output: 10
The main purpose when you use a return statement is that you can break out early. The product variable equals 10 but the if statement will not execute because we just want to return
Method and Local Variable Scope
A method definition creates its own scope outside the regular flow of execution. This is why local variables within a method definition cannot be referenced from outside of the method definition. It’s also the reason why local variables within a method definition cannot access data outside of the method definition (unless the data is passed in as a parameter).
a = 3 def methodName a = 5 end puts a
As you can probably guess, the output is 3 not 5 because of the execution flow and cannot access the local variable inside the method.
Perhaps I will finish this article here. However, if you have any request about this topic or about this programming language just give me a reply below and we will discuss more about it. Thanks for reading. Happy coding!😘