带有ActiveRecord对象的Ruby减运算符

时间:2018-07-05 08:50:43

标签: ruby-on-rails ruby activerecord benchmarking

您能解释一下,Ruby的减号运算符如何工作?不仅是5-2这样的琐碎案件。 稍微复杂一点–我们有2个带有ActiveRecord对象的数组: 数组A = {User.where(...),数组B = User.where(...),我想制作A-B,它是如何工作的?只是比较对象ID或所有属性,还是其他?

2 个答案:

答案 0 :(得分:7)

  

是否只是比较对象ID?

类型和ID,是的。

pry(main)> show-source User#eql?

From: /Users/sergio/.gem/ruby/2.5.1/gems/activerecord-5.2.0/lib/active_record/core.rb @ line 420:
Owner: ActiveRecord::Core
Visibility: public
Number of lines: 6

def ==(comparison_object)
  super ||
    comparison_object.instance_of?(self.class) &&
    !id.nil? &&
    comparison_object.id == id
end

答案 1 :(得分:1)

仅是Sergio的回答中的其他详细信息:

由于我自己也很好奇,我缩小了-方法(运算符)对ActiveRecord::Relation对象的操作范围:

路轨5:

跟踪:

# rails console (pry-rails)
users_a = User.where(...)
users_b = User.where(...)

puts users_a.class
# => `User::ActiveRecord_Relation`

show-source users_a.-

# From: /Users/jrpolidario/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/activerecord-5.2.0/lib/active_record/relation/delegation.rb @ line 41:
# Owner: ActiveRecord::Delegation
# Visibility: public
# Number of lines: 4
#
# delegate :to_xml, :encode_with, :length, :each, :uniq, :join,
#          :[], :&, :|, :+, :-, :sample, :reverse, :rotate, :compact, :in_groups, :in_groups_of,
#          :to_sentence, :to_formatted_s, :as_json,
#          :shuffle, :split, :slice, :index, :rindex, to: :records

# since the `-` method as seen above is delegated to #records, let's see what the return type is the return value of `#records` is, of which is supposed to respond to the `-` operator.

puts users_a.records.class
# => Array

# ...because it's an Array type, then let's see if the Array type responds to the delegated `-` method.

show-source users_a.records.-

# From: array.c (C Method):
# Owner: Array
# Visibility: public
# Number of lines: 17
# 
# static VALUE
# rb_ary_diff(VALUE ary1, VALUE ary2)
# {
#     VALUE ary3;
#     VALUE hash;
#     long i;
# 
#     hash = ary_make_hash(to_ary(ary2));
#     ary3 = rb_ary_new();
# 
#     for (i=0; i<RARRAY_LEN(ary1); i++) {
#   if (st_lookup(rb_hash_tbl_raw(hash), RARRAY_AREF(ary1, i), 0)) continue;
#   rb_ary_push(ary3, rb_ary_elt(ary1, i));
#     }
#     ary_recycle_hash(hash);
#     return ary3;
# }

...这只是意味着,我引用Array

  

返回一个新数组,该数组是原始数组的副本,并删除也出现在other_ary中的所有项目。该顺序保留在原始数组中。

路轨4

P.S。我还尝试在rails 4.2中进行跟踪,并且show-source users_a.-没有显示任何方法,这意味着它使用了method_missing(因此,这方面在4到5之间进行了更改。 ),然后进行进一步的跟踪,结果如下:

跟踪:

[127, 136] in /Users/jrpolidario/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-4.2.10/lib/active_record/relation/delegation.rb
   127: 
   128:     def method_missing(method, *args, &block)
   129:       if @klass.respond_to?(method)
   130:         scoping { @klass.public_send(method, *args, &block) }
   131:       elsif array_delegable?(method)
=> 132:         to_a.public_send(method, *args, &block)
   133:       elsif arel.respond_to?(method)
   134:         arel.public_send(method, *args, &block)
   135:       else
   136:         super

...如您所见,.to_aActiveRecord::Relation对象上被调用,这意味着它成为一个数组,然后是method(即'-')在该Array对象上被调用,这也意味着最后它还调用与上述Rails 5中相同的Array#-方法。