Project

General

Profile

Feature #5899

chaining comparsions.

Added by neleai (Ondrej Bilka) over 5 years ago. Updated over 1 year ago.

Status:
Rejected
Priority:
Normal
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

History

#1 [ruby-core:42150] Updated by Anonymous over 5 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/.

#2 [ruby-core:42157] Updated by kernigh (George Koehler) over 5 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}

#3 [ruby-core:42220] Updated by naruse (Yui NARUSE) over 5 years ago

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

#4 [ruby-core:44014] Updated by matz (Yukihiro Matsumoto) over 5 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.

#5 [ruby-core:49732] Updated by mame (Yusuke Endoh) almost 5 years ago

  • Target version set to next minor

#6 [ruby-core:75177] Updated by sos4nt (Stefan Schüßler) over 1 year 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.

#7 [ruby-core:75544] Updated by nobu (Nobuyoshi Nakada) over 1 year ago

  • Description updated (diff)

#8 [ruby-core:75545] Updated by matz (Yukihiro Matsumoto) over 1 year ago

  • Description updated (diff)

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

Matz.

#9 [ruby-core:75546] Updated by matz (Yukihiro Matsumoto) over 1 year ago

  • Status changed from Feedback to Rejected

#10 [ruby-core:75551] Updated by nobu (Nobuyoshi Nakada) over 1 year ago

  • Description updated (diff)

Also available in: Atom PDF