Automatic Range?

J

Jari Williamsson

Problem is simple:
I want to know if an integer is within the range of x and y. However, I
don't automatically know which of x & y is the largest.

Ruby accepts ranges like (5..2), although there seems to be no real use
for them.

Anyway, is it possible to automatically create a "valid range", or is
this kind of solution the best one?

range = x < y ? (x..y) : (y..x)


Best regards,

Jari Williamsson
 
R

Rob Biedenharn

Problem is simple:
I want to know if an integer is within the range of x and y.
However, I don't automatically know which of x & y is the largest.

Ruby accepts ranges like (5..2), although there seems to be no real
use for them.

Anyway, is it possible to automatically create a "valid range", or
is this kind of solution the best one?

range = x < y ? (x..y) : (y..x)

Best regards,

Jari Williamsson


That's probably as good as it gets. However, Ranges with #end <
#begin are common as arguments to String#[] such as "abcdefg"[2..-2]
=> "cdef".

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
J

Jari Williamsson

Rob said:
However, Ranges with #end < #begin
are common as arguments to String#[] such as "abcdefg"[2..-2] => "cdef".

Ah, of course - there it makes sense. Thanks!


Best regards,

Jari Williamsson
 
R

Robert Dober

Problem is simple:
I want to know if an integer is within the range of x and y. However, I
don't automatically know which of x & y is the largest.

Ruby accepts ranges like (5..2), although there seems to be no real use
for them.

Anyway, is it possible to automatically create a "valid range", or is
this kind of solution the best one?

range = x < y ? (x..y) : (y..x)

[x, y].min .. [x,y].max
is an alternative
Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top