Symbol (programming): Difference between revisions

Content deleted Content added
Added Scheme to list of symbol-using langs
DrPoque (talk | contribs)
No edit summary
Line 1:
A '''symbol''' in [[computer programming]] is a primitive [[datatype]] whose [[instance]]s have a unique human-readable form. Symbols can be used as [[identifiers]]. In some [[programming languages]], that are called '''atoms'''<ref name=pickaxe>{{cite book|last=Hunt|first=Dave Thomas ; Chad Fowler ; Andy|title=Programming Ruby the pragmatic programmers' guide ; [includes Ruby 1.8]|year=2001|publisher=The Pragmatic Bookshelf|___location=Raleigh, NC|isbn=978-0974514055|url=http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html|edition=2. ed., 10. print.}}</ref>.
 
In the most trivial [[implementation]], they are essentially named [[integer]]s.
Line 20:
| [[Ericsson]] [[Erlang (programming language)|Erlang]] || ? || <tt>sym</tt>
|}
 
 
=== Lisp ===
 
Line 56 ⟶ 54:
<source lang="lisp">
package:exported-symbol
</source>
 
===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|accessdate=10 July 2011}}</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>.
====Examples====
The following is a simple example of a symbol literal in Ruby<ref name=pickaxe />:
<source lang=ruby>
my_symbol = :a
my_symbol = :"a"
</source>
Symbols can be [[String interpolation|interpolated]]:
<source lang=ruby>
money = :"#{2+3} dollars" #=> :"5 dollars"
print "I have #{money}" #=> "I have 5 dollars"
</source>
Strings can be coerced into symbols, vice versa:
<source lang=ruby>
my_symbol = "Hello, world!".intern #=> :"Hello, world!"
my_symbol = "Hello, world!".to_sym #=> :"Hello, world!"
my_string = :hello.to_s
</source>
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|accessdate=10 July 2011}}</ref>:
<source lang=ruby>
my_symbol = :hello_world
my_symbol.length #=> 11
my_symbol.class #=> Symbol
my_symbol.to_s.reverse.to_sym #=> :dlrow_olleh
</source>
Symbols as keys of an associative array:
<source lang=ruby>
my_hash = { a: "apple", b: "banana" }
my_hash[:a] #=> "apple"
my_hash[:b] #=> "banana"
</source>
 
Line 62 ⟶ 95:
 
[[Category:Lisp programming language]]
[[Category:Ruby programming language]]
[[Category:Articles with example Ruby code]]
[[Category:Programming constructs]]
{{compu-prog-stub}}