Ruby中的双方括号 [英] Double square brackets in Ruby

查看:111
本文介绍了Ruby中的双方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:

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

双方括号在做什么?

谢谢

推荐答案

这是方法[]的应用,该方法以数组为参数.

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

由于OP不清楚,因此我们无法确定charmap是什么,但是例如,如果它是哈希,则charmap[[char, c = input.getc]]将返回charmap中与键[char, input.getc]相对应的值.

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

这篇关于Ruby中的双方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆