什么是"=〜"? Ruby中的运算符? [英] What is the "=~" operator in Ruby?

查看:90
本文介绍了什么是"=〜"? Ruby中的运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在屏幕录像中看到了这一点,无法弄清楚它是什么.参考表只是将其与其他运算符组合在一起,作为一般的模式匹配运算符.

I saw this on a screencast and couldn't figure out what it was. Reference sheets just pile it in with other operators as a general pattern match operator.

推荐答案

它将字符串与正则表达式匹配.

It matches string to a regular expression.

'hello' =~ /^h/ # => 0

如果没有匹配项,它将返回nil.如果传递无效的参数(即左侧或右侧不正确),它将抛出TypeError或返回false.

If there is no match, it will return nil. If you pass it invalid arguments (ie, left or right-hand sides are not correct), it will either throw a TypeError or return false.

这篇关于什么是"=〜"? Ruby中的运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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