“OrElse”处的 Ruby 等效运算符和“还有” Vb.net 的

发布于 2024-10-14 04:05:49 字数 412 浏览 6 评论 0原文

Ruby 中是否有类似于 VB.NET 中的“OrElse”和“AndAlso”的运算符?

例如,在 Ruby 中,当 active_record 为 nil 时,会引发 NoMethodError 异常:

if active_record.nil? || active_record.errors.count == 0
     ...
end

在 VB.net 中,我可以这样做:

 If active_record Is Nothing OrElse active_record.errors.count = 0
    ...
 End

这不会生成异常,因为它只检查第一个表达式

There are operators in Ruby similar to "OrElse"and "AndAlso" in VB.NET?

For example in Ruby NoMethodError exception is raised when active_record is nil:

if active_record.nil? || active_record.errors.count == 0
     ...
end

In VB.net i can do:

 If active_record Is Nothing OrElse active_record.errors.count = 0
    ...
 End

That does not generate an exception because it is only checked the first expression

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

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

发布评论

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

评论(3

深海少女心 2024-10-21 04:05:49

在这种情况下,不会引发异常(因为只会评估 || 中的第一项)。但是,您可能有兴趣阅读 ActiveSupport 中的 Object#try ,这在处理可能为零的对象时很有帮助。

In this case there will be no exception raised (because only the first term in || will be evaluated). However you might be interested in reading about Object#try from ActiveSupport, which can be helpful when dealing with objects that can be nil.

记忆里有你的影子 2024-10-21 04:05:49

在 Ruby 中,零和未定义的东西之间有很大的区别。考虑以下来自 IRB 的内容:

ruby-1.9.2-p0 :002 > active_record
NameError: undefined local variable or method `active_record' for main:Object
    from (irb):2
    from /Users/jed/.rvm/rubies/ruby-1.9.2-p0/bin/irb:16:in `<main>'
ruby-1.9.2-p0 :003 > active_record = nil
 => nil 
ruby-1.9.2-p0 :004 > active_record.class
 => NilClass 
ruby-1.9.2-p0 :006 > active_record.nil?
 => true 

因此,一个 nil 的对象是 NilClass 的一个实例,因此响应消息 nil? 将返回 true,但不声明变量(如在您的代码中)Ruby不知道你在叫什么。

这里有几个选项:

Ruby 的 || 运算符是严格运算符,而 or 关键字不太严格,所以我不知道 vb 操作与这些操作相比在哪里两个或流选项。

你可以使用一个名为 'andand' 的简洁小 gem,

require 'andand'
active_record.andand.errors.count == 0

但是,通常当你在 Rails 中处理这种情况时,您将使用另一种方法来确定上述情况,请考虑:

@post = Post.new(:my_key => "my value") #=> an ActiveRecord object
if @post.valid?
  # do something meaningful
else
  puts @post.errors.full_messages.to_sentence
end

如果您打算根据某些内容是否可能未定义来分配某些内容,则您需要使用记忆化:

@post ||= Post.new 

如果未定义,它将声明该对象或使用现有对象

in ruby, there is a big difference between something that is nil and something that is undefined. Considering the following, from IRB:

ruby-1.9.2-p0 :002 > active_record
NameError: undefined local variable or method `active_record' for main:Object
    from (irb):2
    from /Users/jed/.rvm/rubies/ruby-1.9.2-p0/bin/irb:16:in `<main>'
ruby-1.9.2-p0 :003 > active_record = nil
 => nil 
ruby-1.9.2-p0 :004 > active_record.class
 => NilClass 
ruby-1.9.2-p0 :006 > active_record.nil?
 => true 

So, an object that is nil is an instance of NilClass and therefore responds to the message nil? will return true, but without declaring the variable (as in your code) Ruby doesn't know what you are calling.

A couple of options here:

Ruby's || operator is a strict operator, whereas the or keyword is less strict, so I don't know where the vb operation compares to these two or flow options.

you could use a neat little gem callled 'andand'

require 'andand'
active_record.andand.errors.count == 0

but, generally when you are dealing with this situation in rails, you would use another means to determine the situation above, consider:

@post = Post.new(:my_key => "my value") #=> an ActiveRecord object
if @post.valid?
  # do something meaningful
else
  puts @post.errors.full_messages.to_sentence
end

and if you mean to assign something based on if it possibly undefined, you would want to use memoization:

@post ||= Post.new 

which will declare the object if undefined or use the existing object

萌能量女王 2024-10-21 04:05:49

Ruby ||短路评估 运算符,所以它应该仅评估第一个条件,因此您的 if 不应引发任何异常。

我假设 active_record.nil? 返回布尔值 true

Ruby || is short circuit evaluation operator, so it should evaluate only first condition, therefore your if should not raise any exception.

I assume active_record.nil? returns boolean true.

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