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

valeriuv
valeriuv
20,999 Points

What is the purpose of symbols?

This lesson showed that sometimes you might use symbols in hashes, as opposed to strings and numbers. However, it is not clear why you might need them? I would appreciate it if someone can clarify.

1 Answer

Trevor J
seal-mask
.a{fill-rule:evenodd;}techdegree
Trevor J
Python Web Development Techdegree Student 2,107 Points

Symbols are most commonly used as placeholders in Hashes. In Rails, for example, the Rails params hash associates any of the values of any parameters passed in by the user (from a form or in the url) with a symbol representing the name of that value.

If you look at the example below from a Rails controller, the create method calls item_params
that uses symbols to store the input pased in from the create item form.

def create
    @item = Item.new(item_params)
end

def item_params
    params.require(:item).permit(:name, :description, :price)
end