Ruby on rails 带有ActiveRecord对象的Ruby减号运算符

Ruby on rails 带有ActiveRecord对象的Ruby减号运算符,ruby-on-rails,ruby,activerecord,benchmarking,Ruby On Rails,Ruby,Activerecord,Benchmarking,请你解释一下,Ruby的负运算符是怎么工作的?不仅仅是像5-2这样的小事。 更复杂一点–我们有两个带有ActiveRecord对象的数组: 数组A=User.where…,数组B=User.where…,我想做个A-B,它是怎么工作的?它只是比较对象ID、所有属性还是其他属性 它只是比较对象ID吗 类型和ID,是的 pry(main)> show-source User#eql? From: /Users/sergio/.gem/ruby/2.5.1/gems/activerecord-

请你解释一下,Ruby的负运算符是怎么工作的?不仅仅是像5-2这样的小事。 更复杂一点–我们有两个带有ActiveRecord对象的数组: 数组A=User.where…,数组B=User.where…,我想做个A-B,它是怎么工作的?它只是比较对象ID、所有属性还是其他属性

它只是比较对象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

仅需了解Sergio回答中的其他细节:

我缩小了-method操作符对ActiveRecord::Relation对象的作用范围,因为我自己也很好奇:

轨道5: 回溯:
。。。正如您所看到的。在ActiveRecord::Relation对象上调用to_a,这意味着它将成为一个数组,然后在该数组对象上调用“-”方法,这也意味着最后它也将调用与上面Rails 5中相同的数组-方法。

1。Ruby“操作符”只是方法。2.用户。在哪里。。。不返回数组,但返回关系。@MarekLipka:并非所有运算符都是方法,但大多数都是包含的。@MarekLipka伙计们,返回的确切位置并不重要,我的意思是,如果我们使用两个带有AR对象的数组:A=[u.find1,u.find2],B=[u.find2,u.find3]。“的减号方法将如何在后台工作?”来自的乔:它使用哈希和eql比较元素?方法提高效率,所以这取决于对象如何实现这些方法。谢谢,这正是我要搜索的。Sry,但我无法改变你的答案:从/Owner/Visibility/LOC输出看起来很有趣-你有提取某些方法的工具吗?@Stefan:huh?那是香草普赖尔。显示源用户。新建==。不过,在本机/C方法上工作得不太好。@SergioTulentsev TIL@engineersmnky:我只是不想每次更改ruby版本时都去安装gem-install-pry-doc。¯\_ツ_/¯
# 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;
# }
[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