88

In Ruby, what's the difference between {} and []?

{} seems to be used for both code blocks and hashes.

Are [] only for arrays?

The documention isn't very clear.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Tilendor
  • 48,165
  • 17
  • 52
  • 58

6 Answers6

75

It depends on the context:

  1. 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
    
  2. [] 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)
    There is also a convention that it is used as a class method in the same way you might use a static Create method in C# or Java. e.g.

    a = {1 => 2} # create a hash for example
    puts a[1] # same as a.fetch(1), will print 2
    
    Hash[1,2,3,4] # this is a custom class method which creates a new hash
    

    See the Ruby Hash docs for that last example.

  3. This is probably the most tricky one - {} is also syntax for blocks, but only when passed to a method OUTSIDE the arguments parens.

    When you invoke methods without parens, Ruby looks at where you put the commas to figure out where the arguments end (where the parens would have been, had you typed them)

    1.upto(2) { puts 'hello' } # it's a block
    1.upto 2 { puts 'hello' } # syntax error, ruby can't figure out where the function args end
    1.upto 2, { puts 'hello' } # the comma means "argument", so ruby sees it as a hash - this won't work because puts 'hello' isn't a valid hash
    
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
  • 2
    side note: Hash#fetch is not exactly Hash#[]. {:a => 1, :b => 2}.fetch(:c) IndexError: key not found – tokland Feb 13 '11 at 12:43
  • @tokland `:c` not found – yyny Dec 27 '16 at 10:58
  • "There is also a convention that it is used as a class method in the same way you might use a static Create method in C# or Java." This is exactly the answer I was looking for. Also a classic example of what I hate most about Ruby; you have to know tons of obscure little tricks to read Ruby code. – Tony Aug 28 '17 at 14:37
23

Another, not so obvious, usage of [] is as a synonym for Proc#call and Method#call. This might be a little confusing the first time you encounter it. I guess the rational behind it is that it makes it look more like a normal function call.

E.g.

proc = Proc.new { |what| puts "Hello, #{what}!" }
meth = method(:print)

proc["World"]
meth["Hello",","," ", "World!", "\n"]
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
sris
  • 4,938
  • 27
  • 26
10

Broadly speaking, you're correct. As well as hashes, the general style is that curly braces {} are often used for blocks that can fit all onto one line, instead of using do/end across several lines.

Square brackets [] are used as class methods in lots of Ruby classes, including String, BigNum, Dir and confusingly enough, Hash. So:

Hash["key" => "value"]

is just as valid as:

{ "key" => "value" }
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
John Topley
  • 113,588
  • 46
  • 195
  • 237
3

The square brackets [ ] are used to initialize arrays. The documentation for initializer case of [ ] is in

ri Array::[]

The curly brackets { } are used to initialize hashes. The documentation for initializer case of { } is in

ri Hash::[]

The square brackets are also commonly used as a method in many core ruby classes, like Array, Hash, String, and others.

You can access a list of all classes that have method "[ ]" defined with

ri []

most methods also have a "[ ]=" method that allows to assign things, for example:

s = "hello world"
s[2]     # => 108 is ascii for e
s[2]=109 # 109 is ascii for m
s        # => "hemlo world"

Curly brackets can also be used instead of "do ... end" on blocks, as "{ ... }".

Another case where you can see square brackets or curly brackets used - is in the special initializers where any symbol can be used, like:

%w{ hello world } # => ["hello","world"]
%w[ hello world ] # => ["hello","world"]
%r{ hello world } # => / hello world /
%r[ hello world ] # => / hello world /
%q{ hello world } # => "hello world"
%q[ hello world ] # => "hello world"
%q| hello world | # => "hello world"
Evgeny
  • 6,533
  • 5
  • 58
  • 64
2

a few examples:

[1, 2, 3].class
# => Array

[1, 2, 3][1]
# => 2

{ 1 => 2, 3 => 4 }.class
# => Hash

{ 1 => 2, 3 => 4 }[3]
# => 4

{ 1 + 2 }.class
# SyntaxError: compile error, odd number list for Hash

lambda { 1 + 2 }.class
# => Proc

lambda { 1 + 2 }.call
# => 3
James A. Rosen
  • 64,193
  • 61
  • 179
  • 261
2

Note that you can define the [] method for your own classes:

class A
 def [](position)
   # do something
 end

 def @rank.[]= key, val
    # define the instance[a] = b method
 end

end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
rogerdpack
  • 62,887
  • 36
  • 269
  • 388