Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

meaning of {} in ruby

When on their own, or assigning to a variable, [] creates arrays, and {} creates hashes. e.g.

a = [1,2,3] # an array
b = {1 => 2} # a hash
[] can be overridden as a custom method, and is generally used to fetch things from hashes (the standard library sets up [] as a method on hashes which is the same as fetch)
a = {1 => 2} # create a hash for example
puts a[1] # same as a.fetch(1), will print 2
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #meaning #ruby
ADD COMMENT
Topic
Name
7+8 =