Ruby 中的类型转换:“正确”的转换 方式?
我正在尝试确定 Ruby 中的字符串是否为数字。 这是我的代码,
whatAmI = "32.3a22"
puts "This is always false " + String(whatAmI.is_a?(Fixnum));
isNum = false;
begin
Float(whatAmI)
isNum = true;
rescue Exception => e
puts "What does Ruby say? " + e
isNum = false;
end
puts isNum
我意识到我可以使用正则表达式来完成它,但是有没有我缺少的标准方法来做到这一点?我见过 can_convert? 方法,不过我好像没有。
有没有办法添加can_convert? 方法到所有字符串? 我知道这在 Ruby 中是可能的。 我也明白这可能是完全不必要的...
编辑 to_f 方法不起作用,因为它们从不抛出异常,而只是在不起作用时返回 0。
I am trying to decide if a string is a number in Ruby. This is my code
whatAmI = "32.3a22"
puts "This is always false " + String(whatAmI.is_a?(Fixnum));
isNum = false;
begin
Float(whatAmI)
isNum = true;
rescue Exception => e
puts "What does Ruby say? " + e
isNum = false;
end
puts isNum
I realize that I can do it with a RegEx, but is there any standard way to do it that I'm missing? I've seen a can_convert? method, but I don't seem to have it.
Is there a way to add a can_convert? method to all Strings? I understand that it's possible in Ruby. I also understand that this may be totally unecessary...
Edit The to_f methods do not work, as they never throw an Exception but rather just return 0 if it doesn't work out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的想法是正确的。 不过,它可以做得更紧凑一些:
内联“救援”非常有趣。 如果您将要保存的部分放在更多内容中间,请将其括起来,例如:
You've got the right idea. It can be done a little more compactly, though:
Inlining "rescue" is great fun. Parenthesize the part you're rescuing if you put it in the middle of more stuff, like:
这可能听起来过于关键,但根据您的描述,您几乎肯定会做一些“错误”的事情...为什么您需要检查 String 是否是
Float
的有效表示而不是实际转换字符串?这只是一种预感,但它可能有助于描述您想要完成的任务。 可能有一个“更好”的方法。
例如,您可能想要将字符串插入数据库并使用字符串连接将 SQL 语句组合在一起。 最好转换为 Float 并使用准备好的语句。
如果您只需要检查字符串的“形状”,那么最好使用正则表达式,因为正如 dylan 指出的那样,Float 可以用多种可能合适或不合适的方式表示。
您可能还需要处理国际输入,在欧洲,使用逗号而不是句点作为小数分隔符,但 Ruby Floats 必须使用 (.)。
This might sound overly critical, but based on your description, you're almost certainly doing something "wrong"... Why would you need to check whether a String is a valid representation of a
Float
but not actually convert the String?This is just a hunch, but it may be useful to describe what you're trying to accomplish. There's probably a "better" way.
For example, you might want to insert the String into a database and put together an sql statement using string concatenation. It would be preferable to convert to a Float and use prepared statements.
If you just need to inspect the "shape" of the string, it would be preferable to use a regular expression, because, as dylan pointed out, a Float can be represented in any number of ways that may or may not be appropriate.
You may also have to deal with international input, in Europe, a comma is used instead of a period as decimal seperator, yet Ruby Floats have to use the (.).
我不知道
can_convert
方法,但通常类会定义to_s
、to_i
、to_hash
等方法>等等。 这些方法返回对象的不同表示形式,例如,to_s 方法将返回对象的字符串表示形式。至于实际的转换,我相信其他人可以比我更好地回答这个问题。
另一方面,如果您想知道是否可以向所有字符串添加一个方法,您可以。 只需打开
String
类并添加它即可!编辑:
to_f
支持包含指数表示法的字符串。I don't know about the
can_convert
method, but generally a class will define methods liketo_s
,to_i
,to_hash
and so on. These methods return a different representation of the object, for example, ato_s
method will return a string representation of an object.As for the actual converting, I'm sure someone else can answer that better than me.
On another note, if you're wondering if you can add a method to all strings, you can. Just open up the
String
class and add it!Edit: Strings containing exponential notation are supported by
to_f
.