Learn Ruby: Method4 min read

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.

Syntax

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 def keyword. After that is your method names, which should begin with lowercase or underscore letter. You should not begin a method with an uppercase because Ruby might think that you’ve just defined a constant hence parse the call incorrectly. At last, your method has to close with end keyword. In Ruby, doesn’t like many other programming languages, you don’t have to put the comma at the end of the statement. 😌

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 want to accept 2 values but you just pass only one, Ruby will display an error. ☹️

Return values from methods

def returnValue
    x = "JavaScript"
    y = "Python"
    z = "Ruby"
end
returnValue

So what is value it will return? x,y or z or all of them? The answer is, when a method called. It will return the last declared variable – z “Ruby”.

Return Statement

We’ve just listed return values beneath, what about the return statement? A lot of people skeptical about return statement is necessary or not. It’ll return one or more values from Ruby’s method. Let’s take a look.

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 product and neglect the rest parts. Now if you try to remove the return statement, the if statement will be executed.

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!😘

Facebook Comments

Previous Article
Next Article

4
Leave a Reply

avatar
2 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
PattrickSindreLearn To Code TogetherGraugh F. Jinkky Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Graugh F. Jinkky
Guest
Graugh F. Jinkky

I probably think Ruby is the language that people should consider learn, but they are not interested in this language much.

Sindre
Guest
Sindre

actually I want that it last little more longer

Pattrick
Guest
Pattrick

agree, anyway I like the way of the author who wrote this article. you can go to ruby.org to learn more if you want

Sign up for newsletter

* indicates required

Categories

Archives