← Back to Upcase

Initializer accepting symbols?


(b4thestorm) #1

I’m currently doing the testing fundamentals trail. I am noticing that the person class that I am testing, has an initializer that has this syntax:

def initialize(first_name: , last_name:) 

I’ve never seen that argument syntax before. Are those symbols in the initializer argument? Or is that something completely different?


(Josh Mills) #2

Yes, those are symbols, using what is called named parameters. You can read more here.

This, in particular, is an implementation of required named parameters, a feature added in Ruby 2.1. See bullet point #4 in A Look at Ruby 2.1


(b4thestorm) #3

Thanks for the resources. I solved it experimenting on repl.it. Keyword arguments seemingly can come in the form:

def some_method(key: )

and that would be valid syntax. but to run it, you can’t do:

some_method('value')

The only way to run the method with the keyword value is with:

some_method(key: value)

Which is pretty intuitive, since it is a key. But not very clear, since its missing its
value. Anyways, thanks dude. I Appreciate the response, that tripped me up.