ruby 1.9.2 有 is_a 吗?功能?

发布于 2024-10-04 07:32:40 字数 165 浏览 8 评论 0原文

我在谷歌上搜索到有一个 is_a? 函数来检查对象是否是整数。

但我在 Rails 控制台中尝试过,但不起作用。

我运行了如下代码:

 "1".is_a?
 1.is_a?

我错过了什么吗?

I googled that there is an is_a? function to check whether an object is an integer or not.

But I tried in rails console, and it doesn't work.

I ran the code like the following:

 "1".is_a?
 1.is_a?

Did I miss something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

横笛休吹塞上声 2024-10-11 07:32:40

您忘记包含您正在测试的类:

"1".is_a?(Integer) # false
1.is_a?(Integer) # true

You forgot to include the class you were testing against:

"1".is_a?(Integer) # false
1.is_a?(Integer) # true
眼波传意 2024-10-11 07:32:40

没有内置函数来判断字符串是否实际上是整数,但您可以轻松地创建自己的函数:

class String
  def int
    Integer(self) rescue nil
  end
end

这​​是可行的,因为如果字符串不能为整数,则内核方法 Integer() 会抛出错误。转换为整数,并且内联的rescue nil 将该错误转换为nil。

Integer("1") -> 1
Integer("1x") -> nil
Integer("x") -> nil

因此:

"1".int -> 1 (which in boolean terms is `true`)
"1x".int -> nil
"x".int -> nil

您可以更改函数以在正确的情况下返回 true,而不是整数本身,但是如果您正在测试字符串以查看它是否是整数,那么您很可能想要使用某个整数!我经常做这样的事情:

if i = str.int
  # do stuff with the integer i
else
  # error handling for non-integer strings
end

虽然如果测试位置的分配冒犯了你,你总是可以这样做:

i = str.int
if i
  # do stuff with the integer i
else
  # error handling for non-integer strings
end

无论哪种方式,这个方法只进行一次转换,如果你必须做很多这样的事情,可能会这样做。具有显着的速度优势。

[将函数名称从 int? 更改为 int 以避免暗示它应该只返回 true/false。]

There's not a built in function to say if a string is effectively an integer, but you can easily make your own:

class String
  def int
    Integer(self) rescue nil
  end
end

This works because the Kernel method Integer() throws an error if the string can't be converted to an integer, and the inline rescue nil turns that error into a nil.

Integer("1") -> 1
Integer("1x") -> nil
Integer("x") -> nil

and thus:

"1".int -> 1 (which in boolean terms is `true`)
"1x".int -> nil
"x".int -> nil

You could alter the function to return true in the true cases, instead of the integer itself, but if you're testing the string to see if it's an integer, chances are you want to use that integer for something! I very commonly do stuff like this:

if i = str.int
  # do stuff with the integer i
else
  # error handling for non-integer strings
end

Although if the assignment in a test position offends you, you can always do it like this:

i = str.int
if i
  # do stuff with the integer i
else
  # error handling for non-integer strings
end

Either way, this method only does the conversion once, which if you have to do a lot of these, may be a significant speed advantage.

[Changed function name from int? to int to avoid implying it should return just true/false.]

一指流沙 2024-10-11 07:32:40

我使用了正则表达式

if a =~ /\d+/
   puts "y"
else
   p 'w'
end

i used a regular expression

if a =~ /\d+/
   puts "y"
else
   p 'w'
end
半窗疏影 2024-10-11 07:32:40

Ruby 有一个名为 respond_to 的函数?可用于查看特定类或对象是否具有具有特定名称的方法。语法类似于

User.respond_to?('name') # returns true is method name exists
otherwise false

http ://www.prateekdayal.net/2007/10/16/rubys-responds_to-for-checking-if-a-method-exists/

Ruby has a function called respond_to? that can be used to seeing if a particular class or object has a method with a certain name. The syntax is something like

User.respond_to?('name') # returns true is method name exists
otherwise false

http://www.prateekdayal.net/2007/10/16/rubys-responds_to-for-checking-if-a-method-exists/

风吹雨成花 2024-10-11 07:32:40

也许这会对你有帮助

str = "1"
=> "1"
num = str.to_i
=> 1
num.is_a?(Integer)
=> true

str1 = 'Hello'
=> "Hello"
num1 = str1.to_i
=> 0
num1.is_a?(Integer)
=> true

Maybe this will help you

str = "1"
=> "1"
num = str.to_i
=> 1
num.is_a?(Integer)
=> true

str1 = 'Hello'
=> "Hello"
num1 = str1.to_i
=> 0
num1.is_a?(Integer)
=> true
尽揽少女心 2024-10-11 07:32:40

我想要类似的东西,但这些都没有为我做到,但这个确实 - 使用“类”:

a = 11
a.class
=> Fixnum

I wanted something similar, but none of these did it for me, but this one does - use "class":

a = 11
a.class
=> Fixnum
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文