Project

General

Profile

Actions

Feature #5798

closed

Range#include? needs some optimization

Added by yimutang (Joey Zhou) over 12 years ago. Updated almost 6 years ago.

Status:
Rejected
Target version:
-
[ruby-core:<unknown>]

Description

For example:

('aa'..'az').include? 123

it seems that the procedure is:

  1. check whether 'aa' == 123 # false
  2. 'aa'.succ # 'ab'
  3. check whether 'ab' == 123 # false
  4. 'ab'.succ # 'ac'
  5. check whether 'ac' == 123 # false
    ...
    n-1. 'ay'.succ # 'az'
    n. check whether 'az' == 123 # false
    finally return false

However, 'aa' and 123 are not the same class. It's not necessary to take the whole steps of 'succ' and '=='.
Maybe it should check 'aa'.class and 123.class first, or use <=> instead of == to check, when 'aa' <=> 123 returns nil(== only returns true/false, no nil), the procedure breaks.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0