1

I encountered this string that looks like JSON syntax but I've never seen a property with a colon before it and not with a backwards greater than or equals sign:

Badge {
    :id=>36, 
    :name=>"Appreciated", 
    :default_icon=>"fa-heart", 
    :badge_type_id=>3, 
    :query=>"", 
    :default_badge_grouping_id=>2, 
    :trigger=>0, 
    :auto_revoke=>false, 
    :system=>true
}

Is it a JSON string and if so what does the colon do?

  • I think that's Ruby. Where did you find the code? – Gregory Nisbet Apr 9 at 1:20
  • It was output to the console during an upgrade of Discourse – 1.21 gigawatts Apr 9 at 1:43
  • github.com/discourse/discourse ... Discourse is about 65% ruby by weight (according to GitHub). – Gregory Nisbet Apr 9 at 1:45
  • 1
    Badge is the name of the class. I updated my answer with a link to the file that defines the class. The hash literal is valid Ruby, but a human would be more likely to use the key: value syntax. – Gregory Nisbet Apr 9 at 1:51
2

Update:

Knowing more about the context (message written to console during an updgrade of discourse), you likely saw an instance of this class being printed.


I think you are looking at Ruby code, although it is strange to mix symbol literals and =>.

  • An identifier beginning with a colon is a :symbol literal.
  • A key-value pair in a hash table can be written key => value or key: value, but the latter syntax only works with symbol literals.
  • Badge doesn't have parentheses after it and you can omit parentheses.

Some other languages use => to separate key value pairs, here's why the other likely suspects don't fit.

  • Perl: A key begining with : must be quoted. Bare identifiers and bare identifiers with a leading - are the only things that do not need to be quoted in Perl in this context.
  • PHP: PHP allows {} for accessing array values $a{6}, but does not allow braces for array literals. Also, you can't leave the parentheses off a function call in PHP.
1

Colon variable refers to :abc type variables you might have seen in Ruby. They are called Ruby symbols.

A Ruby symbol is not a variable because it cannot be assigned a value.

Also, a Ruby symbol is not a reference to another variable nor is it a pointer to a memory location.

Still a symbol has a value and whenever the name of the symbol is same, its value is the same.

An example will make it more clear.

# It is trivial to assign a value to a variable.
abc = "1"
=> "1"

# But a symbol cannot be assigned any value.
:a = "1"
# SyntaxError: A symbol cannot be assigned a value

# Can use a variable as a map-key 
m = {abc => "1"}
=> {"1"=>"1"}

# Can use a string as a map-key 
=> {"def"=>"1"}

# Can also use a symbol as a map-key 
m = {:a => "1"}
=> {:a=>"1"}

# Can use same symbol as key in another map
m2 = {:a => "2"}
=> {:a=>"2"}

# And it won't affect the previous map.
m
=> {:a=>"1"}

m2
=> {:a=>"2"}
  • So is a symbol comparable to a constant? – 1.21 gigawatts Apr 12 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.