Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass argument to the method definition of `[]`?

Tags:

methods

ruby

How can I pass an argument to the definition of a singleton method of [] on A, in other words A['some_argument']?

class A
  def self.[]
    # some_argument
  end
end
like image 536
shaan Avatar asked Jan 24 '19 11:01

shaan


People also ask

How do you pass an argument to a method?

To allow a method to modify a argument, you must pass in an object. Objects in Java are also passed by value, however, the value of an object is a reference. So, the effect is that the object is passed in by reference. When passing an argument by reference, the method gets a reference to the object.

How do you pass an argument to a method in Java?

Parameters and ArgumentsInformation can be passed to methods as parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

How can we pass argument to the method by reference?

And there are two ways to pass the parameters in functions: call by value and call by reference. C/C++ supports the call by reference because in the call by reference we pass the address of actual parameters in the place of formal parameters using Pointers.

How many ways can arguments be passed in methods?

Two ways to pass arguments to methods in many programming languages are pass-by-value and pass-by-reference. When an argument is passed by value (the default in C#), a copy of its value is made and passed to the called method.

How do you pass a method from one method to another?

We can't directly pass the whole method as an argument to another method. Instead, we can call the method from the argument of another method. Here, the returned value from method2 () is assigned as an argument to method1 (). If we need to pass the actual method as an argument, we use the lambda expression.

How do you pass an argument to a method?

In C#, there are two basic ways to pass an argument to a method: passing by value. In this case, the argument value is copied into the formal parameter of the method, as described in the previous paragraph. With this method, all changes over the formal parameter that occur in the method do not affect the argument that was used in the call;

What is the difference between parameters and arguments in a method?

The parameters are used in the method body and at runtime will take on the values of the arguments that are passed in. Note: Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked.

How do you pass a function as an argument in Python?

All functions in Python can be passed as an argument to another function. Methods are passed as arguments just like a variable. In this example, we define a class and its objects. We create an object to call the class methods.


2 Answers

Just as to any other method, using argument list:

class A 
  def self.[](arg)
    puts arg
  end  
end  

A[1]
# => 1
like image 104
mrzasa Avatar answered Sep 20 '22 07:09

mrzasa


Good answers are already given, but I think they miss to mention an important point.

The method [], when used ordinarily in the form foo[params] is actually in syntax sugar form. The underlying method name is [], and calling it in the underlying form would be foo.[](params).

Syntax sugar plays around with syntax, and transforms a method call in the form foo[params] into foo.[](params). But that does not work in method definition, so you have to define such method in the underlying form, not in the syntax sugar form.

like image 44
sawa Avatar answered Sep 18 '22 07:09

sawa