Quantcast
Channel: What do the different brackets in Ruby mean? - Stack Overflow
Browsing all 7 articles
Browse latest View live

Answer by rogerdpack for What do the different brackets in Ruby mean?

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 endend

View Article



Answer by sris for What do the different brackets in Ruby mean?

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...

View Article

Answer by Evgeny Zislis for What do the different brackets in Ruby mean?

The square brackets [ ] are used to initialize arrays.The documentation for initializer case of [ ] is inri Array::[]The curly brackets { } are used to initialize hashes.The documentation for...

View Article

Answer by Orion Edwards for What do the different brackets in Ruby mean?

It depends on the context:When on their own, or assigning to a variable, [] creates arrays, and {} creates hashes. e.g.a = [1,2,3] # an arrayb = {1 => 2} # a hash[] can be overridden as a custom...

View Article

Answer by James A. Rosen for What do the different brackets in Ruby mean?

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...

View Article


Answer by John Topley for What do the different brackets in Ruby mean?

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...

View Article

What do the different brackets in Ruby mean?

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.

View Article
Browsing all 7 articles
Browse latest View live




Latest Images