I came across some Ruby code,
I try to understand why the variables have colon at the end of their name inside the declaration of the initialize
method.
Is there any reason for the colon?
attr_reader :var1, :var2
def initialize(var1:, var2:)
@var1 = var1
@var2 = var2
end
Those are keyword arguments.
You can use them by name and not position. E.g.
ThatClass.new(var1: 42, var2: "foo")
or
ThatClass.new(var2: "foo", var1: 42)
An article about keyword arguments by thoughtbot
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With