Project

General

Profile

Feature #16137

Add === to UnboundMethod

Added by okuramasafumi (Masafumi OKURA) 9 months ago. Updated 9 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:94758]

Description

Abstract

UnboundMethod class should have === so that it can be used in case statement.

Background

Method class has === method so that we can do something like:

require 'prime'
case 11
when Prime.method(:prime?) then :prime
end

However, we cannot do something like this:

positive = Integer.instance_method(:positive?)
case 11
when positive then :positive
end

Proposal

Add === method to UnboundMethod class.

Implementation

Minimal implementation in Ruby could be:

class UnboundMethod
  def ===(other)
    bind(other).call
  end
end

Summary

=== for UnboundMethod can improve readability in case statement.

Also available in: Atom PDF