Re: difference between :label and variable
Matthew Kerwin <[email protected]> Fri, 1 Nov 2019 17:02:24 +1000
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CACweHNAqr7VwzOveN46WryYeD1ZnGZXPgq+rA=nZMC1rCJa4Dg@mail.gmail.com> |
A Symbol (like `:label`) is just another data type. It can be
assigned to a variable the same as a String or Integer or whatever.
```
# an immediate Symbol
:foo
# a variable whose value is a Symbol
x = :bar
# a hash
{
:foo => nil,
x => nil,
}
```
You get the idea.
A Symbol is supposed to represent one of the simplest data types, in
that its only value is its identity, and its only operation is
identity-comparison. (In other words :foo is always exactly equal to
:foo, and nothing else.) It doesn't support addition, or
concatenation, or iteration, etc.
However over time people have treated them like "magic strings", so
the strictness of that definition is slipping slightly.
Cheers
On Fri, 1 Nov 2019 at 10:58, Maggie Q Roth <[email protected]> wrote:
>
> dear members,
>
> what's the difference between :label and variable?
> I saw many hashes use :label as key, which has advantages than variable keys?
>
> Thank you.
> Maggie
>
> Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
--
Matthew Kerwin
https://matthew.kerwin.net.au/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>