Symbol (programming): Difference between revisions

Content deleted Content added
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
 
(4 intermediate revisions by 2 users not shown)
Line 13:
| [[ANSI]] [[Common Lisp]] || symbol, keyword || <code>symbol</code>, <code>:keyword</code>
|-
| [[Clojure]] || symbol,<ref>[http://clojure.org/data_structures#Data%20Structures-Symbols Symbols] on the page on Data Structures</ref> keyword<ref>[http://clojure.org/data_structures#Data%20Structures-Keywords Keywords] on the page on Data Structures</ref> || <code>'symbol</code>, <code>:keyword</code>
|-
| [[Dart (programming language)|Dart]] || Symbol<ref>{{cite web |title=A tour of the Dart language {{pipe}} Symbols |url=https://dart.dev/guides/language/language-tour#symbols |website=Dart programming language |access-date=17 January 2021}}</ref> || <code>#sym</code>
Line 39:
| [[Scala (programming language)|Scala]] || scala.Symbol || <code>'symbol</code>
|-
| [[Scheme (programming language)|Scheme]] || symbol || <code>symsymbol</code>
|-
| [[Smalltalk]] || Symbol || <code>#sym</code> or <code>#'sym'</code>
Line 111:
===Ruby===
In [[Ruby (programming language)|Ruby]], symbols can be created with a literal form, or by converting a string.<ref name=pickaxe />
They can be used as an identifier or an interned string.<ref name="rubysymbol">{{cite web |last=Kidd |first=Eric |date=20 January 2007 |title=13 Ways of Looking at a Ruby Symbol |url=http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#9 |work=Random Hacks |access-date=10 July 2011}}</ref> Two symbols with the same contents will always refer to the same object.<ref>{{Cite web|url=httphttps://docs.ruby-doclang.comorg/docsen/ProgrammingRubymaster/Symbol.html|title=Programming Ruby: The Pragmatic Programmer's Guide|website=ruby-doc.com}}</ref>
It is considered a [[best practice]] to use symbols as keys to an [[associative array]] in Ruby.<ref name=rubysymbol /><ref name="wrongreason">{{cite web|title=Using Symbols for the Wrong Reason|url=http://microjet.ath.cx/WebWiki/2005.12.27_UsingSymbolsForTheWrongReason.html|work=Gnomic Notes}}</ref>
 
Line 129:
=> "hello"
</syntaxhighlight>
Symbols are objects of the <code>Symbol</code> class in Ruby:<ref name="rdocsymbol">{{cite web|title=Symbol|url=httphttps://wwwdocs.ruby-doclang.org/coreen/classesmaster/Symbol.html|work=Ruby Documentation|access-date=10 July 2011}}</ref>
<syntaxhighlight lang=irb>
irb(main):004:0> my_symbol = :hello_world