1
class Object
  def all_equals(o)
    ops = [:==, :===, :eql?, :equal?]
    Hash[ops.map(&:to_s).zip(ops.map {|s| send(s, o) })]
  end
end

输出

"a".all_equals "a" # => {"=="=>true, "==="=>true, "eql?"=>true, "equal?"=>false}

任何人都可以通过尽可能深入地破坏代码来帮助我,看看它是如何产生这样的输出的吗?

只是想知道给出一些输出的逻辑

谢谢

4

1 回答 1

3
self
# => "a"

o
# => "a"

ops.map(&:to_s)
# => [:==, :===, :eql?, :equal?].map(&:to_s)
# => ["==", "===", "eql?", "equal?"]

ops.map {|s| send(s, o) }
# => [:==, :===, :eql?, :equal?].map {|s| "a".send(s, "a") }
# => ["a".send(:==, "a"), "a".send(:===, "a"), "a".send(:eql?, "a"), "a".send(:equal?, "a")]
# => ["a" == "a", "a" === "a", "a".eql?("a"), "a".equal?("a")]
# => [true, true, true, false]

ops.map(&:to_s).zip(ops.map {|s| send(s, o) })
# => ["==", "===", "eql?", "equal?"].zip([true, true, true, false])
# => [["==", true], ["===", true], ["eql?", true], ["equal?", false]]

Hash[ops.map(&:to_s).zip(ops.map {|s| send(s, o) })]
# => Hash[[["==", true], ["===", true], ["eql?", true], ["equal?", false]]]
# => {"==" => true, "===" => true, "eql?" => true, "equal?" => false}
于 2013-01-12T21:12:37.707 回答