As we know we see *args
as an argument in method definition it means we can pass
multiple parameters.
See the below example
1 2 3 4 5 6 7 8 |
|
But what if we just have asterisk(splat operator) as an argument?
I was looking Rails code base and found asterisk(*)
in an class initialize method.
Link: https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb#L42
Let’s take an example –:
1 2 3 4 5 |
|
How many arguments you passed it wont generate any error
Let’s take another example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
But now if we pass *
as an argument in child class initialize method see what
happens.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
You can see passing * as an argument help to send parameters for parent class even though child class doesn’t require those arguments
Note: *
is called splat operator in Ruby.
See some sample Rails code which is using *
https://github.com/rails/rails/blob/v4.0.0/railties/lib/rails/commands/server.rb#L40 https://github.com/rack/rack/blob/1.5.2/lib/rack/server.rb#L178
It was new to me so thought to share with you guys. I hope you can get it if you do not know about this already.