home PYTHONJAVA
 

Ruby method

Ruby methods are similar to functions in other programming languages. Ruby methods are used to bundle one or more repeated statements into a single unit.

Method names should begin with a lowercase letter. If you start with a capital letter as the method name, Ruby might treat it as a constant, causing the call to be parsed incorrectly.

The

method should be defined before the call, otherwise Ruby will produce an undefined method call exception.

grammar

def method_name [( [arg [= default]]...[, * arg [, &expr ]])] expr.. end

So you can define a simple method like this:

def method_name expr.. end

You can define a method to accept parameters as follows:

def method_name (var1, var2) expr.. end

You can set a default value for the parameter, or use the default value if the method is called without passing the required parameter:

def method_name (var1=value1, var2=value2) expr.. end

When you want to call a method, just use the method name as follows:

method_name

However, when you call a method with parameters, you also need to include the parameter when writing the method name, for example:

method_name 25 , 30

The biggest disadvantage of using the parameterized method is that you need to remember the number of parameters when calling the method. For example, if you pass only two parameters to a method that accepts three arguments, Ruby displays an error.

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- def test(a1="Ruby", a2="Perl") puts "Programming language is#{a1}" puts "Programming language is#{a2}" end test "C", "C++" test

The above example runs the output as:

The programming language is C
The programming language is C++
The programming language is Ruby
The programming language is Perl

Return value from method

Each method in Ruby returns a value by default. The value returned is the value of the last statement. For example:

Instance

def test i = 100 j = 10 k = 0 end

When this method is called, the last declared variable k will be returned.

Ruby return statement

The return statement in Ruby is used to return one or more values from a Ruby method.

Syntax

return [ expr[`, ' expr...]]

If more than two expressions are given, the array containing those values will be the return value. If no expression is given, nil will be the return value.

Instance

return or return 12 orreturn 1,2,3

Look at the example below:

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- def test i = 100 j = 200 k = 300 return i, j, k end var = test puts var

The above example runs the output as:

100
200
300

variable number of parameters

Assuming you declare a method with two arguments, you also need to pass two arguments when you call the method.

However, Ruby allows you to declare methods with a variable number of parameters. Let's take a look at the following example:

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- def sample (*test) puts " The number of parameters is#{test.length}" for i in 0...test.length puts " Parameter value#{test[i]}" end end sample "Zara", "6", "F" sample "Mac", "36", "M", "MCA"

In this code, you have declared a method sample that accepts a parameter test. However, this parameter is a variable parameter. This means that parameters can have different numbers of variables. The output of the above example runs as follows:

The number of parameters is 3
Parameter value Zara
Parameter value 6
Parameter value F
The number of parameters is 4
Parameter value Mac
Parameter value 36
Parameter value M
Parameter value MCA

Ruby undef statement

This statement is used to cancel the method definition. undef cannot appear in the body of the method.

By using undef and alias, the interface of a class can be modified independently from the parent class, but note that it may break the program when it is called inside its own method.

undef Method Name

Instance

The following example cancels the method definition named bar:

undef bar





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.