<p>In Ruby, what's the difference between <code>{}</code> and <code>[]</code>?</p>

<p><code>{}</code> seems to be used for both code blocks and hashes.</p>

<p>Are <code>[]</code> only for arrays?</p>

<p>The documention isn't very clear.</p>

[{'answer_id': 12575, 'author': 'John Topley', 'author_id': 1450, 'author_profile': 'https://Stackoverflow.com/users/1450', 'pm_score': 3, 'selected': False, 'text': '<p>Broadly speaking, you\'re correct. As well as hashes, the general style is that curly braces <code>{}</code> are often used for blocks that can fit all onto one line, instead of using <code>do</code>/<code>end</code> across several lines.</p>\n\n<p>Square brackets <code>[]</code> are used as class methods in lots of Ruby classes, including String, BigNum, Dir and confusingly enough, Hash. So:</p>\n\n<pre><code>Hash["key" =&gt; "value"]\n</code></pre>\n\n<p>is just as valid as:</p>\n\n<pre><code>{ "key" =&gt; "value" }\n</code></pre>\n'}, {'answer_id': 12608, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 2, 'selected': False, 'text': '<p>a few examples:</p>\n\n<pre><code>[1, 2, 3].class\n# =&gt; Array\n\n[1, 2, 3][1]\n# =&gt; 2\n\n{ 1 =&gt; 2, 3 =&gt; 4 }.class\n# =&gt; Hash\n\n{ 1 =&gt; 2, 3 =&gt; 4 }[3]\n# =&gt; 4\n\n{ 1 + 2 }.class\n# SyntaxError: compile error, odd number list for Hash\n\nlambda { 1 + 2 }.class\n# =&gt; Proc\n\nlambda { 1 + 2 }.call\n# =&gt; 3\n</code></pre>\n'}, {'answer_id': 13935, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 7, 'selected': True, 'text': '<p>It depends on the context:</p>\n\n<ol>\n<li><p>When on their own, or assigning to a variable, <code>[]</code> creates arrays, and <code>{}</code> creates hashes. e.g.</p>\n\n<pre><code>a = [1,2,3] # an array\nb = {1 =&gt; 2} # a hash\n</code></pre></li>\n<li><p><code>[]</code> can be overridden as a custom method, and is generally used to fetch things from hashes (the standard library sets up <code>[]</code> as a method on hashes which is the same as <code>fetch</code>) <br>\nThere is also a convention that it is used as a class method in the same way you might use a <code>static Create</code> method in C# or Java. e.g.</p>\n\n<pre><code>a = {1 =&gt; 2} # create a hash for example\nputs a[1] # same as a.fetch(1), will print 2\n\nHash[1,2,3,4] # this is a custom class method which creates a new hash\n</code></pre>\n\n<p>See the Ruby <a href="http://www.ruby-doc.org/core-2.0/Hash.html#method-c-5B-5D" rel="noreferrer">Hash docs</a> for that last example.</p></li>\n<li><p>This is probably the most tricky one -\n<code>{}</code> is also syntax for blocks, but only when passed to a method OUTSIDE the arguments parens.</p>\n\n<p>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)</p>\n\n<pre><code>1.upto(2) { puts \'hello\' } # it\'s a block\n1.upto 2 { puts \'hello\' } # syntax error, ruby can\'t figure out where the function args end\n1.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\n</code></pre></li>\n</ol>\n'}, {'answer_id': 80928, 'author': 'Evgeny', 'author_id': 11414, 'author_profile': 'https://Stackoverflow.com/users/11414', 'pm_score': 2, 'selected': False, 'text': '<p>The square brackets [ ] are used to initialize arrays.\nThe documentation for initializer case of [ ] is in</p>\n\n<pre><code>ri Array::[]\n</code></pre>\n\n<p>The curly brackets { } are used to initialize hashes.\nThe documentation for initializer case of { } is in</p>\n\n<pre><code>ri Hash::[]\n</code></pre>\n\n<p>The square brackets are also commonly used as a method in many core ruby classes, like Array, Hash, String, and others.</p>\n\n<p>You can access a list of all classes that have method "[ ]" defined with</p>\n\n<pre><code>ri []\n</code></pre>\n\n<p>most methods also have a "[ ]=" method that allows to assign things, for example:</p>\n\n<pre><code>s = "hello world"\ns[2]     # =&gt; 108 is ascii for e\ns[2]=109 # 109 is ascii for m\ns        # =&gt; "hemlo world"\n</code></pre>\n\n<p>Curly brackets can also be used instead of "do ... end" on blocks, as "{ ... }".</p>\n\n<p>Another case where you can see square brackets or curly brackets used - is in the special initializers where any symbol can be used, like:</p>\n\n<pre><code>%w{ hello world } # =&gt; ["hello","world"]\n%w[ hello world ] # =&gt; ["hello","world"]\n%r{ hello world } # =&gt; / hello world /\n%r[ hello world ] # =&gt; / hello world /\n%q{ hello world } # =&gt; "hello world"\n%q[ hello world ] # =&gt; "hello world"\n%q| hello world | # =&gt; "hello world"\n</code></pre>\n'}, {'answer_id': 710327, 'author': 'sris', 'author_id': 83996, 'author_profile': 'https://Stackoverflow.com/users/83996', 'pm_score': 5, 'selected': False, 'text': '<p>Another, not so obvious, usage of <code>[]</code> 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.</p>\n\n<p>E.g.</p>\n\n<pre><code>proc = Proc.new { |what| puts "Hello, #{what}!" }\nmeth = method(:print)\n\nproc["World"]\nmeth["Hello",","," ", "World!", "\\n"]\n</code></pre>\n'}, {'answer_id': 1563105, 'author': 'rogerdpack', 'author_id': 32453, 'author_profile': 'https://Stackoverflow.com/users/32453', 'pm_score': 2, 'selected': False, 'text': '<p>Note that you can define the <code>[]</code> method for your own classes:</p>\n\n<pre><code>class A\n def [](position)\n   # do something\n end\n\n def @rank.[]= key, val\n    # define the instance[a] = b method\n end\n\nend\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/12565', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1470/']