如何在 ruby​​ on rails 2 中检查对象是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10371673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:19:47  来源:igfitidea点击:

How i can check if an object is null in ruby on rails 2?

ruby-on-railsrubyruby-on-rails-2

提问by George Panayi

I want to check when my object @objectnameis not equal to null to show the values of the @objectnameelse to show that no values found.

我想检查我的对象何时@objectname不等于 null 以显示@objectnameelse的值以显示未找到值。

I tried this:

我试过这个:

<% if (@objectname != null) then %>

but I'm getting an error.

但我遇到了一个错误。

回答by Pavel Veller

it's nilin Ruby, not null. And it's enough to say if @objectnameto test whether it's not nil. And no then. You can find more on ifsyntax here:

nil在 Ruby 中,而不是null. 并且说到if @objectname测试是否不为nil就够了。没有then。您可以在if此处找到有关语法的更多信息:

http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Control_Structures#if

http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Control_Structures#if

回答by lesce

You can check if an object is nil (null) by calling present? or blank? .

您可以通过调用 present? 来检查对象是否为 nil (null)?还是空白?.

@object.present?

this will return false if the project is an empty string or nil .

如果项目是空字符串或 nil ,这将返回 false 。

or you can use

或者你可以使用

@object.blank?

this is the same as present? with a bang and you can use it if you don't like 'unless'. this will return true for an empty string or nil .

这和现在一样吗?一声巨响,如果您不喜欢“除非”,则可以使用它。这将为空字符串或 nil 返回 true 。

回答by Chloe

Now with Ruby 2.3 you can use &.operator ('lonely operator') to check for nilat the same time as accessing a value.

现在使用 Ruby 2.3,您可以在访问值的同时使用&.运算符('lonely operator')进行检查nil

@person&.spouse&.name

https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators#Other_operators

https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators#Other_operators



Use #tryinstead so you don't have to keep checking for nil.

#try改为使用,这样您就不必继续检查nil.

http://api.rubyonrails.org/classes/Object.html#method-i-try

http://api.rubyonrails.org/classes/Object.html#method-i-try

@person.try(:spouse).try(:name)

instead of

代替

@person.spouse.name if @person && @person.spouse

回答by Joshua Cheek

In your example, you can simply replace nullwith `nil and it will work fine.

在你的例子中,你可以简单地null用 `nil替换它,它会正常工作。

require 'erb'

template = <<EOS
<% if (@objectname != nil) then %>
  @objectname is not nil
<% else %>
  @objectname is nil
<% end %>
EOS

@objectname = nil
ERB.new(template, nil, '>').result # => "  @objectname is nil\n"

@objectname = 'some name'
ERB.new(template, nil, '>').result # => "  @objectname is not nil\n"

Contrary to what the other poster said, you can see above that thenworks fine in Ruby. It's not common, but it is fine.

与另一张海报所说的相反,您可以在上面看到它then在 Ruby中运行良好。这不常见,但很好。

#blank?and #present?have other implications. Specifically, if the object responds to #empty?, they will check whether it is empty. If you go to http://api.rubyonrails.org/and search for "blank?", you will see what objects it is defined on and how it works. Looking at the definition on Object, we see "An object is blank if it's false, empty, or a whitespace string. For example, “”, “ ”, nil, [], and {} are all blank."You should make sure that this is what you want.

#blank?#present?有其他影响。具体来说,如果对象响应#empty?,他们将检查它是否为空。如果您访问http://api.rubyonrails.org/并搜索“空白?”,您将看到它被定义在哪些对象上以及它是如何工作的。查看Object上的定义,我们看到“如果对象为 false、空或空白字符串,则该对象为空。例如,“”、“ ”、nil、[] 和 {} 都是空的。” 你应该确保这是你想要的。

Also, nil is considered false, and anything other than false and nil is considered true. This means you can directly place the object in the if statement, so a more canonical way of writing the above would be

此外,nil 被认为是假的,除 false 和 nil 之外的任何事情都被认为是真的。这意味着您可以直接将对象放在 if 语句中,因此编写上述内容的更规范的方式是

require 'erb'

template = <<EOS
<% if @objectname %>
  @objectname is not nil
<% else %>
  @objectname is nil
<% end %>
EOS

@objectname = nil
ERB.new(template, nil, '>').result # => "  @objectname is nil\n"

@objectname = 'some name'
ERB.new(template, nil, '>').result # => "  @objectname is not nil\n"

If you explicitly need to check niland not false, you can use the #nil?method, for which nil is the only object that will cause this to return true.

如果您明确需要检查niland not false,则可以使用该#nil?方法,对于该方法, nil 是唯一会导致 this 返回 true 的对象。

nil.nil?          # => true
false.nil?        # => false
Object.new.nil?   # => false

回答by Vasanth Saminathan

You can use the simple not flag to validate that. Example

您可以使用简单的 not 标志来验证它。例子

if !@objectname

This will return true if @objectname is nil. You should not use dot operator or a nil value, else it will throw

如果@objectname 为nil,这将返回true。你不应该使用点运算符或 nil 值,否则它会抛出

*** NoMethodError Exception: undefined method `isNil?' for nil:NilClass

*** NoMethodError 异常:未定义的方法“isNil?” 对于零:NilClass

An ideal nil check would be like:

理想的 nil 支票是这样的:

!@objectname || @objectname.nil? || @objectname.empty?