What do the different brackets in Ruby mean?

前端 未结 6 2067
萌比男神i
萌比男神i 2020-12-04 16:40

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

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

Are []

6条回答
  •  半阙折子戏
    2020-12-04 17:04

    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
    

提交回复
热议问题