Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如果有价值,就做点什么_Ruby On Rails_Ruby_Activesupport - Fatal编程技术网

Ruby on rails 如果有价值,就做点什么

Ruby on rails 如果有价值,就做点什么,ruby-on-rails,ruby,activesupport,Ruby On Rails,Ruby,Activesupport,我经常发现自己在编写Ruby代码时会检查是否存在值,然后在有值的情况下使用该值做一些事情。例如 if some_object.some_attribute.present? call_something(some_object.some_attribute) end 我想如果它可以写成 some_object.some_attribute.presence { |val| call_something(val) } => the return value of call_someth

我经常发现自己在编写Ruby代码时会检查是否存在值,然后在有值的情况下使用该值做一些事情。例如

if some_object.some_attribute.present?
  call_something(some_object.some_attribute)
end
我想如果它可以写成

some_object.some_attribute.presence { |val| call_something(val) }
=> the return value of call_something
有人知道Ruby或activesupport中是否有这样的功能吗

我为该功能打开了一个应用程序。

您可以试试

do_thing(object.attribute) if object.attribute
这通常很好,除非属性是布尔值。在这种情况下,如果值为false,它将不会调用

如果属性可能为false,请改用
.nil?

do_thing(object.attribute) unless object.attribute.nil?

您可以使用和的组合:

如果调用
try
时不带参数,则除非它是
nil
,否则它将向给定块生成接收器:


虽然没有现成的功能,但可以做到:

some_object.some_attribute.tap do |attr|
  attr.present? && call_smth(attr) 
end
另一方面,Rails提供了如此多的monkeypatches,人们可以在这个马戏团中附加一个:

class Object
  def presense_with_rails
    raise 'Block required' unless block_given?
    yield self if self.present? # requires rails
  end
  def presense_without_rails
    raise 'Block required' unless block_given?
    skip = case self
           when NilClass, FalseClass then true
           when String, Array then empty?
           else false
           end
    yield self unless skip
  end
end

如果您只是在寻找一个单行程序,那么可以使用一个内联If语句:
call\u something(some\u object.some\u attribute)If some\u object.some\u attribute.present?
这将调用
some\u attribute
两次,这对于冗长性和性能原因是不可取的(If
some\u attribute
调用IO)
attr=是否存在某个对象、某个属性?call_something(attr)end
这将仅在不需要说“Update”时调用属性。如果有必要,我们可以知道你是否修改了这个问题。相反,只需将您的附加信息添加到问题的正常流程中。您也可以编写
一些对象。一些属性。呈现并调用一些东西(一些对象。一些属性)
。我知道,但是我想避免两次引用某个对象。某个属性。除非该属性实际上是一个正在执行一些繁重任务的方法,否则两次调用它的开销很小,它是一段已经存储在内存中的数据,因此执行起来很快。您可以将其设置为局部变量,虽然只有两种用途,这真的没有多大帮助。或者,您可以在
call\u something
方法中添加nil检查,使调用代码更干净。e、 g.
def调用某物(attr);如果attr.nil?返回#主体;结束
出于各种原因,我不喜欢
尝试
,但这是对这个问题的一个很好的回答。很好,但前提是你在铁路上非常酷-而且你似乎可以忽略
在场
,直接调用
尝试
。@nsave,
尝试
可以通过拉入相应的主动支持核心库,使它非常轻量级,并且在非Rails代码中易于访问。见@NielsB。不太可能,在我的第二个示例中,
'.try(&:upcase)
将执行块并返回其结果,而
'.presence.try(&:upcase)
不会。为什么不改用它呢。它重量轻,经过良好的测试和支持。几年前,他们将主动支持分解成小块,这样我们就可以挑选我们想要的,并且可以依靠AS维护人员来保持其工作。@theTinMan
try
是一个不错的选择,我只是展示了一种可能使用的技术。顺便说一句,我对斯特凡的回答投了赞成票。
class Object
  def presense_with_rails
    raise 'Block required' unless block_given?
    yield self if self.present? # requires rails
  end
  def presense_without_rails
    raise 'Block required' unless block_given?
    skip = case self
           when NilClass, FalseClass then true
           when String, Array then empty?
           else false
           end
    yield self unless skip
  end
end