What is the use of :var in Ruby?

Questions by StephenRaj   answers by StephenRaj

Showing Answers 1 - 4 of 4 Answers

gikie

  • Dec 14th, 2009
 

:var : Semicolon followed by an identifier tells us that var is symbol.

Two strings with same content may belong to two totally different objetcs but if you convert these two strings to symbol, both of them belongs to the same symbol object.

If a string is used as a unique identifier then it is always good to define it as a symbol.

  Was this answer useful?  Yes

vijay prasad

  • Jul 22nd, 2011
 

In Ruby the :var is used to define a symbol. A symbol ii ruby is created by adding 'colon' in-front of variable name .You can also create symbol by transforming a string to symbol using string#intern.

For example:

Code
  1.   name[:first_name]='vijay'

  2.   name['last_name'.intern]=' Prasad'

  3.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions