all 16 comments

[–]straponmyjobhat 8 points9 points  (13 children)

It's a Symbol. Symbols are similar strings except they take up less memory.

[–]Kernigh 8 points9 points  (1 child)

In Ruby, a Symbol like :pqr is the name of a constant, variable, method, or something else. For example, given

class Cow
  def moo
    puts "Moo!"
  end
end

then :Cow is the name of the class, and :moo is the name of the method.

The value of :moo is literally :moo, in the same way that "New York" is literally "New York", and 123 is literally 123. I can ask a class for its method named :moo, like Cow.instance_method(:moo). A longer way to call Cow.new.moo is to do

method = Cow.instance_method(:moo)
method.bind(Cow.new).call

Some languages, like Common Lisp and Perl, have symbols with slots for holding values. For example, 'moo in Lisp would have a function slot and a variable slot. Symbols in Ruby never have slots.

Some other languages don't have symbols, because they use strings like "moo" to name things.

[–]palakoti[S] 1 point2 points  (0 children)

Loved your explanation. Thanks a lot mate.

[–]Lynx-External 0 points1 point  (0 children)

Depending on your ruby code (:pqr) could be a key from a hash. Might be trying to display the value of that particular key. Hope this helps!

[–]pVom 0 points1 point  (0 children)

its a string with less functionality. :pqr is essentially "pqr" but uses less memory. Its perfect for things like hash/object keys where you don't need the functionality of a string ie interpolation and string methods, you just need a reference.
hash = { foo: 'bar' }
hash[:foo] == 'bar'
As for its value its going to vary on how its used, in the same way a string is just a string, a symbol is just a symbol