What's the colon for in a Hash?

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 key :file is a symbol, in this case.

···


Sent from Mailbox

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:
http://www.troubleshooters.com/codecorn/ruby/symbols.htm

···

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

Thanks guys! I'm off to learn more now. :slight_smile:

Leam

···

--
Mind on a Mission

Read this -- http://stackoverflow.com/questions/20249420/colon-in-arguments-of-a-method-call-ruby/20249436#20249436 .If it helps.

···

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.

--Brian Kernighan

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:
http://www.troubleshooters.com/codecorn/ruby/symbols.htm

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
>

--
        Eric Christopherson

As of Ruby 2.2, they are now! See:

  https://www.ruby-lang.org/en/news/2014/12/25/ruby-2-2-0-released/

-Dave

···

On Sun, Mar 1, 2015 at 3:08 PM, Eric Christopherson <echristopherson@gmail.com> wrote:

symbols are never garbage-collected,

--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.

As of Ruby 2.2, they are now! See:

  https://www.ruby-lang.org/en/news/2014/12/25/ruby-2-2-0-released/

-Dave

You are true and also worth to mention **Most symbols which are returned by String#to_sym and
String#intern are GC-able.** (https://github.com/ruby/ruby/blob/v2_2_0/NEWS#L41)

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:

symbols are never garbage-collected,

hello,

It os an interesting and usefull article there
http://www.troubleshooters.com/codecorn/ruby/symbols.htm
Thanks, i wil try to translate it to my words (in castellano)

···

2015-03-02 9:01 GMT+01:00 Arup Rakshit <aruprakshit@rocketmail.com>:

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:

> symbols are never garbage-collected,

As of Ruby 2.2, they are now! See:

  https://www.ruby-lang.org/en/news/2014/12/25/ruby-2-2-0-released/

-Dave

You are true and also worth to mention **Most symbols which are returned
by String#to_sym and
String#intern are GC-able.** (
https://github.com/ruby/ruby/blob/v2_2_0/NEWS#L41)

Regards,
Arup Rakshit

--
Equipo

EscenarioDeJuego <http://EscenarioDeJuego.blogspot.com.es/>