Project

General

Profile

Actions

Feature #16137

closed

Add === to UnboundMethod

Added by okuramasafumi (Masafumi OKURA) over 5 years ago. Updated 11 months ago.

Status:
Closed
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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0