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

OUTPUT

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

Can anyone help me by breaking the code as much as deep can to see how it gave such output?

Just wanted to know the logic how it works to give some output

Thanks

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317

1 Answers1

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}
sawa
  • 165,429
  • 45
  • 277
  • 381
  • `+1` to you to show your interest and and explanation! that's the logic was running behind of the code! okay i understood! – Arup Rakshit Jan 12 '13 at 21:17