Jump to content

The ultimate community for Ruby on Rails developers.


Photo

newbie question

ruby

  • Please log in to reply
6 replies to this topic

#1 rubyontrain

rubyontrain

    Passenger

  • Members
  • 1 posts

Posted 30 October 2013 - 10:00 PM

I am confused where " : " is used sometimes before the variables and sometimes after the variables like

:product and sometimes it becomes product:   ... what is the meaning of these notations ? can anyone please tell me ?

 

 

thanks



#2 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 31 October 2013 - 06:11 AM

Variables with a : before them are symbols

:symbol

Think of these like constants.

 

When working with hashes, you'll see stuff like

hash = { :key => 'value' }

Here the symbols are used as keys. In Ruby 1.9 they introduced a shorthand for the above, namely

hash = { key: 'value' }

which is the same way Javascript defines objects and hashes.


  • james and Rowel like this

Blog: http://ohm.sh | Twitter: madsohm


#3 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 31 October 2013 - 08:05 PM

^ what he said :) 

 

I prefer using the shorthand method, seems clearer in intent... and less typing too. 



#4 orrymr

orrymr

    Signalman

  • Members
  • 11 posts
  • LocationJohannesburg, South Africa

Posted 20 January 2014 - 06:18 AM

Variables with a : before them are symbols

:symbol

Think of these like constants.

 

When working with hashes, you'll see stuff like

hash = { :key => 'value' }

Here the symbols are used as keys. In Ruby 1.9 they introduced a shorthand for the above, namely

hash = { key: 'value' }

which is the same way Javascript defines objects and hashes.

 

In the shorthand notation, is key: still a symbol, despite the fact that it doesn't start with a colon?



#5 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 20 January 2014 - 06:20 AM

In the shorthand notation, is key: still a symbol, despite the fact that it doesn't start with a colon?

 

Yes. You can see this by firing up an IRB and writing

{ key: 'value' }

this will return 

=> {:key=>"value"}

  • orrymr likes this

Blog: http://ohm.sh | Twitter: madsohm


#6 orrymr

orrymr

    Signalman

  • Members
  • 11 posts
  • LocationJohannesburg, South Africa

Posted 20 January 2014 - 06:33 AM

Ok, so does that mean that in

<%= form_for :post, url: posts_path do |f| %> 

:url => posts_path is a hash (key/value pair?)



#7 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 20 January 2014 - 04:39 PM

Ok, so does that mean that in

<%= form_for :post, url: posts_path do |f| %> 

:url => posts_path is a hash (key/value pair?)

 

Yes, and if you'd like, you could write it:

<%= form_for :post, :url => posts_path do |f| %>

This is however the "old" syntax


  • orrymr likes this

Blog: http://ohm.sh | Twitter: madsohm






Also tagged with one or more of these keywords: ruby

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users