Explain :colon and colon: in Ruby Like I'm Five

I am learning rails, finished ruby basics. Still confused with colon before and after in ruby.

 def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }

This is a create method automatically generated by rails scaffold command.

and please explain whats going on with the code.

Did you find this post useful? Show some love!

:colon and colon: are both symbols in Ruby, a type of data (like strings, integers, etc).

As this Stack Exchange answer says, symbols are immutable. The link explains it better than I would so I'll leave that for them.

In the context of Ruby and Rails, you won't have to know too much besides some syntax rules:

# in a hash:
my_hash = {
  :cool => :symbol
# is the same as...
my_hash = {
  cool: :symbol

In previous versions of Ruby, the rocket or arrow syntax was the only way to have a symbol key point to a symbol value. Now, you can do a short-hand version using the after colon syntax colon:.

One thing to note is that if you ever want a hash with a key that is a string, you have to use the arrow/rocket syntax:

# this will work
my_hash = {
  "cool" => :thing
#=> {"cool" => :thing }

# this will not work
my_hash = {
  "cool": :thing
#=> { :cool => :thing }
# converts the string "cool" to a symbol

For more info about that code, I recommend reading the Rails guide section about scaffolding, which explains the code generated via scaffolding.

just doing a nitpick addition :-) :

actually, this works just fine: {"symbol-key-with-dash": :cool}. its that you can write a Symbol literal with quotes: :"looks-weird-still-is-symbol"

as a newcomer, my first "whoa" effect was when i realized that every function call like render :show, status: :created, location: @user is ultimately translated to: render(:show, {:status => :created, :location => @user});. it's just that in ruby you don't have to specify that curly braces to make the last argument a Hash. ruby is implicit. you can omit the brackets, the semicolon.

Oh interesting! Didn't know that. I personally think it's a bit confusing to have do "string": or :"string"if you wanted to have a key as a String instead of a Symbol. Good point to bring up though.

Classic DEV Post from Mar 3

Tools you need to use in your react components development 

As a react developer, I believe that everyone who is working on a react project must develop all the components separately for taking advantage of that components philosophy behind this amazing front-end framework.

Follow @elaziziyoussouf to see more of their posts in your feed.
Aswath KNM
Member since Jul 11, 2017
More from @aswathm78
Explain ReasonML Like I'm Five
Explain like I'm five. What is a process and what is thread ?
#explainlikeimfive #process #discuss
Trending on dev.to
Find someone to mentor
#productivity #career #devto
Who's looking for open source contributors? (October 15th edition)
#discuss #opensource #hacktoberfest
The Most Important Non-Programming Skills for Programmers
#career #leadership #tips #culture
I'm a software developer and a meditation teacher - ask me anything!
#ama #productivity #softskills
Ways to earn from Open Source Project as an Owner
#healthydebate #discuss #hacktoberfest
Dev.to Review #4: Top 7 Of The Week, Discussed
#discuss #audio #bestofdev #popular
Let's talk about the state of auto-generated documentation tools for JavaScript
#discuss #javascript #tools
How Can I Help Foster A Culture of Continual Learning?
#discuss #learning