Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Write Our First Tests

Difference between :title and title:

Hey everyone, I was wondering if there was a difference between :title and title:

I was fiddling around with them in Class TodoList < ActiveRecord::Base

validates :title, presence: true -> This runs fine

validates title:, presence: true -> This breaks

Any reason why? Thanks in advance everyone!

3 Answers

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Symbols in Ruby look like this - :symbol. In the past, hashes that used symbols in Ruby looked like this - :key => :value.

Ruby nowadays also lets you use this form instead, where you put the colon at the end of the key and ignore the => symbol - key: :value (the value still retains the starting colon if it's a symbol). As far as I know, this is the only situation when you can use the ending colon - on a symbol key, when it's in a hash. presence: true is a hash.

Thank you for your response! :) So you are saying the key value still retains its symbol properties in this new format ?

Thanks

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Yes. It's still a symbol, but moving the colon lets you use the new format and ignore the => rocket.

Ethan Lowry
PLUS
Ethan Lowry
Courses Plus Student 7,323 Points

Hey Alex,

This can get a little confusing and is somewhat awkward to explain but I'll try and say it as simply as possible.

Words with a : at the front are known an Ruby as symbols. Generally speaking, symbols should always have the colon at the beginning, as in your first working example.

The only exception to this is in the common scenario where you are using symbols as the keys in a Hash. For example:

{ symbol_1: 'some value', symbol_2: 'some other value' }

This is a hash in which the keys are symbols, and the values are strings.

This style of hash is actually a shorthand introduced in more recent versions of Ruby, to replace the old style below (note that these symbols below have the colon at the start now):

{ :symbol_1 => 'some_value', :symbol_2 => 'some other value' }

These hashes are exactly the same - it's simply neater looking and best practice to use the first version, so you should.

You'll see this done all the time, such as when passing arguments to a function as a hash, such as:

Person.create(name: 'Ethan', age: 23)

I hope that makes it clearer - feel free to ask if you're still not sure.

Thank you! That makes sense actually

Thank you Maciej Czuchnowski .. you have cleared it up for me!