[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Automatic Range?

Jari Williamsson

12/16/2007 12:16:00 AM

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

4 Answers

Rob Biedenharn

12/16/2007 12:26:00 AM

0

On Dec 15, 2007, at 7:15 PM, Jari Williamsson wrote:
> 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://agileconsult...
Rob@AgileConsultingLLC.com



Jari Williamsson

12/16/2007 8:49:00 AM

0

Rob Biedenharn wrote:
> 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

Robert Dober

12/16/2007 8:58:00 AM

0

On Dec 16, 2007 1:15 AM, Jari Williamsson
<jari.williamsson@mailbox.swipnet.se> wrote:
> 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

--

http://ruby-smalltalk.blo...

---
All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

yansimon52

11/13/2013 1:57:00 PM

0

U watch tis evenig news or not...Capitaland jusr launched a condo at redhill where 150 units were snapped in one day during today launching.

Needless to say..."kar ki nan" Capital is always able to secure d best location frm Land Office (SLA)..
Are we talking abt "fair level playing field" in sg?