Double square brackets in Ruby

淺唱寂寞╮ 提交于 2019-12-23 14:35:03

问题


Given the following code:

def map(char, charmap)
    unless map = charmap[[char]]
     unless map = charmap[[char, c = input.getc]]
       input.ungetc(c) if c
       map = ''
     end
    end
  map
end

What is the double square brackets doing?

Thanks


回答1:


It is application of the method [] taking an array as the argument.

Since the OP did not make clear, we cannot tell what charmap is, but for example if it were a hash, then charmap[[char, c = input.getc]] would return the value in charmap that corresponds to the key [char, input.getc].



来源:https://stackoverflow.com/questions/20361770/double-square-brackets-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!