测试 Ruby 字符串或符号的字符串相等性(不是对象相等性)的最简洁方法? [英] Most concise way to test string equality (not object equality) for Ruby strings or symbols?

查看:42
本文介绍了测试 Ruby 字符串或符号的字符串相等性(不是对象相等性)的最简洁方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是这样做来测试 Ruby 中的字符串相等性:

I always do this to test string equality in Ruby:

if mystring.eql?(yourstring)
 puts "same"
else
 puts "different"
end

这是在不测试对象相等性的情况下执行此操作的正确方法吗?

Is this is the correct way to do this without testing object equality?

我正在寻找最简洁的方法来根据字符串的内容进行测试.

I'm looking for the most concise way to test strings based on their content.

加上括号和问号,这看起来有点笨拙.

推荐答案

根据 http://www.techotopia.com/index.php/Ruby_String_Concatenation_and_Comparison

做任何一个

mystring == yourstring

mystring.eql? yourstring

是等价的.

这篇关于测试 Ruby 字符串或符号的字符串相等性(不是对象相等性)的最简洁方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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