Project

General

Profile

Actions

Feature #5899

closed

chaining comparsions.

Added by neleai (Ondrej Bilka) almost 13 years ago. Updated over 8 years ago.

Status:
Rejected
Target version:
-
[ruby-core:42149]

Description

Migration from python to ruby is problematic because ruby lacks certain features.
Most important python advantage is chained comparison
We must wrap every comparison like described below.

puts "yes" if 1<2<3<4

class Fixnum
  def <(a)
    ((self<=>a)==-1)&&a
  end
end
class FalseClass
  def <(a)
    false
  end
end

Updated by Anonymous almost 13 years ago

Ondrej Bilka wrote in post #1041080:

Issue #5899 has been reported by Ondrej Bilka.


Feature #5899: chaining comparsions.
https://bugs.ruby-lang.org/issues/5899

puts "yes" if 1<2<3<4

+1

--
Posted via http://www.ruby-forum.com/.

Updated by kernigh (George Koehler) almost 13 years ago

What is a good way to do chained comparison in Ruby?

My best attempt, so far, is

[1, 2, 3, 4].each_cons(2).all? {|a, b| a < b}

Updated by naruse (Yui NARUSE) almost 13 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

Updated by matz (Yukihiro Matsumoto) over 12 years ago

  • Status changed from Assigned to Feedback

I don't think changing "1 < b < 4" to "1 < b && b < 4" is that hard task.
I am not sure it's worth allowing confusing "false < 4".

Matz.

Updated by mame (Yusuke Endoh) about 12 years ago

  • Target version set to 2.6

Updated by sos4nt (Stefan Schüßler) over 8 years ago

Yukihiro Matsumoto wrote:

I don't think changing "1 < b < 4" to "1 < b && b < 4" is that hard task.

Writing 1 < b && b < 4 is not hard. But doesn't 1 < b < 4 look intriguing? It's so clean!

I would also expect a slight difference, namely that 1 < b < 4 evaluates b once and 1 < b && b < 4 evaluates b twice.

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

  • Description updated (diff)

Updated by matz (Yukihiro Matsumoto) over 8 years ago

  • Description updated (diff)

I don't think it's worth adding extra complexity to Ruby.

Matz.

Updated by matz (Yukihiro Matsumoto) over 8 years ago

  • Status changed from Feedback to Rejected

Updated by nobu (Nobuyoshi Nakada) over 8 years ago

  • Description updated (diff)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0