如果#(x =〜/\d/).nil是什么意思? #return非数字的索引?我完全不明白 [英] what is the meaning in if #(x =~ /\d/).nil? #return the index of the non-digit ? I quite don't get it

查看:118
本文介绍了如果#(x =〜/\d/).nil是什么意思? #return非数字的索引?我完全不明白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

input = ARGV 
 
class RPNCalculator
 
    def evaluate(expression)
        to_evaluate = []
        a = expression.split(" ")
        a.each do |x|
            if (x =~ /\d/).nil? #return the index of the non-digit
                to_count = to_evaluate.pop(2)
                to_evaluate << to_count[0].send(x, to_count[1])
            else
                to_evaluate << x.to_i
            end
        end
        to_evaluate[0]
    end
end
 
 
calc = RPNCalculator.new
p calc.evaluate(input)

推荐答案

如果char不是数字,那个表达式的计算结果为 true



x =〜/ \d / 运行正则表达式 [ ^ ] \d :如果字符串中有一个数字(在这种情况下只有1个字符),则该表达式返回数字的索引。如果它不包含数字,则返回 nil



.nil? 检查 x =〜/ \d / 是否等于 nil 。如果是,则返回true。如果没有,则返回false。
That expression evaluates to true if the char is not a digit.

x =~ /\d/ runs the regular expression[^] \d on the char: if there is a digit in the string (which is, in this case, only 1 char), this expression returns the index of the digit. If it does not contain a digit, it returns nil.

.nil? checks whether x =~ /\d/ equals nil. If it does, it returns true. If it does not, it returns false.


这篇关于如果#(x =〜/\d/).nil是什么意思? #return非数字的索引?我完全不明白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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