Symbol (programming): Difference between revisions

Content deleted Content added
m Reverted 1 edit by 2A02:C7F:4482:9000:BD02:569:6CA8:1D27 (talk) to last revision by BlackholeWA
Monkbot (talk | contribs)
m Task 18 (cosmetic): eval 6 templates: del empty params (1×); hyphenate params (2×);
Line 87:
 
===Prolog===
In [[Prolog (programming language)|Prolog]], symbols (or atoms) are the primary primitive data types, similar to numbers.<ref name=Bratko2001>{{Cite book | last1 = Bratko | first1 = Ivan | title = Prolog programming for artificial intelligence | year = 2001 | publisher = Addison Wesley | ___location = Harlow, England ; New York | isbn = 978-0-201-40375-6 | pages = }}</ref> The exact notation may differ in different Prolog's dialects. However, it is always quite simple (no quotations or special beginning characters are necessary).
 
Contrary to other languages, it is possible to give symbols some ''meaning'' by creating some Prolog's facts and/or rules.
Line 102:
===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|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|accessdateaccess-date=10 July 2011}}</ref> Two symbols with the same contents will always refer to the same object.<ref>{{Cite web | url=http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UI | title=Programming Ruby: The Pragmatic Programmer's Guide}}</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 120:
=> "hello"
</syntaxhighlight>
Symbols are objects of the <code>Symbol</code> class in Ruby:<ref name="rdocsymbol">{{cite web|title=Symbol|url=http://www.ruby-doc.org/core/classes/Symbol.html|work=Ruby Documentation|accessdateaccess-date=10 July 2011}}</ref>
<syntaxhighlight lang=irb>
irb(main):004:0> my_symbol = :hello_world