What Are Symbols in Ruby on Rails?
March 20, 2020
What is a symbol in Ruby?
The syntax for symbols is a colon then a word
In Ruby on Rails it's common for symbols to be used to identify methods, instance varibles and hash keys
params.require(:user).permit(:name, :email, :password)
user is refering to the
@user instance variable that get created when you create a new user. The
:password symbols refer to the attributes of the User model.
Instead of accessing using
user["password"] you can use use
passwordto make the syntax cleaner.
Also, I found out in my research that since every time you call a symbol is call the same object, so it's only one instance running, but with something like a string the same sting will create multiple instances and use more memory.