On Fri, Feb 27, 2015 at 9:14 AM, leam hall <leamhall@gmail.com> wrote:
Trying to understand some code. Not sure what the colon ":" is for in
the hash key. Anything I can read up on to understand it?
Thanks!
Leam
####
require 'optparse'
options = {}
OptionParser.new do |opt|
opt.on('-f', '--file file') { |o| options[:file] = o }
end.parse!
puts "File is #{options[:file]}."
####
--
Mind on a Mission
The colon indicates that the hash key is a symbol. Symbols are often used
in hashes as keys because a symbol will always have the same object_id no
matter what, whereas a string like 'file' will have different object_id's
each time it is defined.
Check out this link:
···
On Fri, Feb 27, 2015 at 11:13 AM, leam hall <leamhall@gmail.com> wrote:
Trying to understand some code. Not sure what the colon ":" is for in
the hash key. Anything I can read up on to understand it?
Thanks!
Leam
####
require 'optparse'
options = {}
OptionParser.new do |opt|
opt.on('-f', '--file file') { |o| options[:file] = o }
end.parse!
On Friday, February 27, 2015 12:13:54 PM leam hall wrote:
Trying to understand some code. Not sure what the colon ":" is for in
the hash key. Anything I can read up on to understand it?
Thanks!
Leam
####
require 'optparse'
options = {}
OptionParser.new do |opt|
opt.on('-f', '--file file') { |o| options[:file] = o }
--
Regards,
Arup Rakshit
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
The colon indicates that the hash key is a symbol. Symbols are often used
in hashes as keys because a symbol will always have the same object_id no
matter what, whereas a string like 'file' will have different object_id's
each time it is defined.
Note, however, that a string makes a fine hash key -- if you have three
separate String objects s1, s2, and s3, and they all have the same
value, even if they have different object IDs, h[s1] will retrieve the
same value as h[s2] and h[s3]. However, they will be stored as three
separate objects in memory. All symbols with the same value will be
stored as only one object in memory, so if your program uses a given key
lots of times, you can expect to save some memory versus using string
keys; but on the other hand, symbols are never garbage-collected, so if
your program uses a lot of distinct symbols, they'll always remain in
memory, even if they're no longer needed.
···
On Fri, Feb 27, 2015, Jordan Limbach wrote:
On Fri, Feb 27, 2015 at 11:13 AM, leam hall <leamhall@gmail.com> wrote:
> Trying to understand some code. Not sure what the colon ":" is for in
> the hash key. Anything I can read up on to understand it?
>
> Thanks!
>
> Leam
>
> ####
> require 'optparse'
>
> options = {}
> OptionParser.new do |opt|
> opt.on('-f', '--file file') { |o| options[:file] = o }
> end.parse!
>
> puts "File is #{options[:file]}."
> ####
>
> --
> Mind on a Mission
>
You are true and also worth to mention **Most symbols which are returned by String#to_sym and
String#intern are GC-able.** (ruby/NEWS at v2_2_0 · ruby/ruby · GitHub)
Regards,
Arup Rakshit
···
On Monday, 2 March 2015 2:38 AM, Dave Aronson <ruby-talk.list.2.TRex@codosaur.us> wrote:
On Sun, Mar 1, 2015 at 3:08 PM, Eric Christopherson <echristopherson@gmail.com> wrote:
You are true and also worth to mention **Most symbols which are returned
by String#to_sym and
String#intern are GC-able.** ( ruby/NEWS at v2_2_0 · ruby/ruby · GitHub)