YouTip LogoYouTip

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 start with a lowercase letter. If you start a method name with an uppercase letter, Ruby may treat it as a constant, leading to incorrect parsing of the call. Methods should be defined before they are called; otherwise, Ruby will raise an undefined method call exception. ## Syntax def method_name[([arg]...[, * arg[, &expr]])]expr.. end So, you can define a simple method as follows: def method_name expr.. end You can define a method that accepts parameters as follows: def method_name(var1, var2)expr.. end You can set default values for parameters, which will be used if the required parameters are not passed during the method call: def method_name(var1=value1, var2=value2)expr.. end When you want to call a method, you just need to use the method name, as follows: method_name However, when you call a method with parameters, you need to include the parameters along with the method name, for example: method_name 25, 30 The biggest drawback of using methods with parameters is that you need to remember the number of parameters when calling the method. For example, if you pass only two arguments to a method that accepts three parameters, Ruby will display an error. ## Example def test(a1="Ruby", a2="Perl")puts"Programming language is #{a1}"puts"Programming language is #{a2}"end test"C", "C++"test [Try it Β»](#) The output of the above example is: Programming language is C Programming language is C++Programming language is RubyProgramming language is Perl Every method in Ruby returns a value by default. The returned value is the value of the last statement. For example: ## Example def test i = 100 j = 10 k = 0 end When this method is called, it will return the last declared variable, k. 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, an array containing these values will be the return value. If no expression is given, nil will be the return value. ## Example return or return 12 or return 1,2,3 Look at the following example: ## Example def test i = 100 j = 200 k = 300 return i, j, k end var = test puts var [Try it Β»](#) The output of the above example is: 100200300 Suppose you declare a method with two parameters; when you call that method, you also need to pass two arguments. However, Ruby allows you to declare methods with a variable number of parameters. Let's look at the following example: ## Example def sample(*test)puts"The number of arguments is #{test.length}"for i in 0...test.length puts"The argument value is #{test}"end end sample"Zara", "6", "F"sample"Mac", "36", "M", "MCA" [Try it Β»](#) In this code, you have declared a method `sample` that accepts one parameter `test`. However, this parameter is a variable parameter. This means the parameter can have a different number of variables. The output of the above example is: The number of arguments is 3The argument value is ZaraThe argument value is 6The argument value is F The number of arguments is 4The argument value is MacThe argument value is 36The argument value is M The argument value is MCA When a method is defined outside a class, it is marked as _private_ by default. On the other hand, if a method is defined inside a class, it is marked as public by default. The default visibility of methods and the _private_ marking can be changed using the _public_ or _private_ methods of the module (Module). When you want to access a class's methods, you first need to instantiate the class. Then, using the object, you can access any member of the class. Ruby provides a way to access methods without instantiation. Let's see how to declare and access a class method: class Accounts def reading_charge end def Accounts.return_date end end We already know how the method `return_date` is declared. It is declared by following the class name with a dot, and then the method name. You can access the class method directly, as follows: Accounts.return_date To access this method, you do not need to create an object of the `Accounts` class. This statement is used to alias a method or a global variable. Aliases cannot be defined within a method body. Even if the method is overridden, the alias retains the current definition of the method. Aliasing numbered global variables ($1, $2,...) is prohibited. Overriding built-in global variables may cause serious problems. ### Syntax alias method_name method_name alias global_variable global_variable ## Example alias foo bar alias$MATCH$& Here, we have defined an alias `foo` for `bar`, and an alias `$MATCH` for `$&`. This statement is used to undefine a method definition. _undef_ cannot appear within a method body. By using _undef_ and _alias_, the interface of a class can be modified independently of its parent class, but note that it may break the program if internal method calls are involved. undef method_name ### Example The following example undefines the method named _bar_: undef bar
← Ruby BlockAtt Audio Preload β†’