Project

General

Profile

Actions

Bug #13762

closed

Change in `#==` in Ruby 2.4?

Added by backus (John Backus) over 7 years ago. Updated over 7 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
[ruby-core:82141]

Description

Given this code:

# frozen_string_literal: true

class MyProxy < BasicObject
  def initialize(target)
    @target = target
  end

  undef_method :==

  def method_missing(method_name, *args, &block)
    if target_respond_to?(method_name)
      @target.__send__(method_name, *args, &block)
    else
      ::Object
        .instance_method(method_name)
        .bind(@target)
        .call(*args, &block)
    end
  end

  private

  def target_respond_to?(method_name)
    ::Object
      .instance_method(:respond_to?)
      .bind(@target)
      .call(method_name, true)
  end
end

puts 'a' == MyProxy.new('a')
puts MyProxy.new('a') == 'a'
puts MyProxy.new('a') == MyProxy.new('a')

The output differs depending on the Ruby version:

$ chruby 2.3.4
$ ruby test.rb
true
true
true
$ chruby 2.4.1
$ ruby test.rb
false
true
false

Files

test.rb (739 Bytes) test.rb backus (John Backus), 07/24/2017 12:29 AM

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

  • Description updated (diff)
  • Status changed from Open to Rejected

When you write your custom method_missing, you have to write your custom respond_to_missing? too.

Actions

Also available in: Atom PDF

Like0
Like0