In Ruby, I see a method definition as follows:
def [](param) # do stuff end
What does the declaration of this method mean? How it works? When to use it? And how to call this type of method with an instance of an object?
This is the name of the method, [] . Perhaps you already know Array#[] or Hash#[] . In your classes, you can also define such a method. What he will do is up to you.
[]
Array#[]
Hash#[]
class Foo def [](param) # body end end f = Foo.new f[:some_value]
This means that the method is called " [] ". You call it like any other method:
a = ['foo', 'bar', 'baz'] a.[](1) # => 'bar'
In addition, for methods with this name, you can also call them as
a[1] # => 'bar'
Have you read this Array # [] ? This will give you some idea about this.
[1,2].[](1) # => 2 | | ---------> <--------- method name argument
Source: https://habr.com/ru/post/952935/More articles:Error PHP Codeigniter could not open stream: permission denied - filebrew doctor gives python configuration warning: should these configuration files be deleted? (Mac) - pythonUse withCredential with $ resource - javascriptHow to make ttk.Treeview strings editable? - pythonDate Range Picker, how to trigger an event when entering a date - javascriptAndroid JSONObject: add an array to put method - javawhat is the average difference between Nest sockets and redis-namespace when we use redis with rails / rubies - ruby-on-railsJava JsonObject array value for key - javaCombining + andPredicateWithSubpredicates: and + orPredicateWithSubpredicates: in one predicate - objective-cInheritance with AndroidAnnotations - androidAll Articles