Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Cute open range?

Reply
Thread Tools

Cute open range?

 
 
Clifford Heath
Guest
Posts: n/a
 
      02-15-2008
So I wanted to allow folk using my DSL to say "0..N",
"1..N", etc, to describe the cardinality of a relationship,
and I needed to find a useful way to define N without
making it a fixed integer maximum value.

Well, I came up with this, which I thought was cute:

class N
def self.coerce(n)
[ n, n+1 ] # Anything you can do, I can do better
end
end

Now when I ask (0..N) === m, for any m, it says "true",
and when I say the following, it never ends:

(0..N).each do |n|
puts n
end

But more importantly, I can say:

do_something if range.last == N

Ruby is sweeet .

Clifford Heath.
 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      02-15-2008
Clifford Heath wrote:
> So I wanted to allow folk using my DSL to say "0..N",
> "1..N", etc, to describe the cardinality of a relationship,
> and I needed to find a useful way to define N without
> making it a fixed integer maximum value.
>
> Well, I came up with this, which I thought was cute:
>
> class N
> def self.coerce(n)
> [ n, n+1 ] # Anything you can do, I can do better
> end
> end
>
> Now when I ask (0..N) === m, for any m, it says "true",
> and when I say the following, it never ends:
>
> (0..N).each do |n|
> puts n
> end
>
> But more importantly, I can say:
>
> do_something if range.last == N
>
> Ruby is sweeet .


Does that do anything that Infinity doesn't do?

irb(main):001:0> Infinity = 1/0.0
=> Infinity
irb(main):002:0> (0..Infinity) === 3
=> true
irb(main):003:0> (0..Infinity) === -1
=> false
irb(main):004:0> (0..Infinity).each do |n|
irb(main):005:1* p n
irb(main):006:1> break if n > 3
irb(main):007:1> end
0
1
2
3
4
=> nil
irb(main):008:0> puts "do_something" if (0..Infinity).last == Infinity
do_something
=> nil


Also, there's this....

class N
def self.coerce(n)
[ n, n+1 ]
end
end

x = 3 + N
p x # ==> 7 w.t.f.?

Infinity = 1/0.0
x = 3 + Infinity
p x # ==> Infinity

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
 
 
 
Clifford Heath
Guest
Posts: n/a
 
      02-15-2008
Joel VanderWerf wrote:
> Does that do anything that Infinity doesn't do?


Hey, I forgot about Infinity, much better idea,
and you get -Infinity too . But this does do
one thing that Infinity doesn't: it works correctly
with Bignums that are too big to be coerced to a
float. I don't think that should concern me though .

Is there a standard place where Infinity is defined,
or should I risk re-defining it? Or worse, test and
define only if needed...

Clifford Heath.
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      02-15-2008
On 15.02.2008 07:34, Clifford Heath wrote:
> Joel VanderWerf wrote:
>> Does that do anything that Infinity doesn't do?

>
> Hey, I forgot about Infinity, much better idea,
> and you get -Infinity too . But this does do
> one thing that Infinity doesn't: it works correctly
> with Bignums that are too big to be coerced to a
> float. I don't think that should concern me though .
>
> Is there a standard place where Infinity is defined,
> or should I risk re-defining it? Or worse, test and
> define only if needed...


Why define or redefine? Why not just

irb(main):001:0> N = 1/0.0
=> Infinity
irb(main):002:0> (1..N) === 2
=> true
irb(main):003:0> (1..N) === -2
=> false

etc? I believe this is what Joel wanted to suggest.

Kind regards

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Very Cute ... but annoying ... Thom Sørensen Computer Support 14 11-05-2008 09:32 PM
Explanation for Error Codes in Cute FTP 5.0 Rama Krishna.G.V MCSE 1 11-08-2004 02:49 PM
Cant get a <div> to display news from Cute News. Talimore HTML 4 07-18-2004 01:49 AM
Well, isn't this cute. (NOT.) Mara Computer Support 13 10-01-2003 10:12 AM
Re: Cute "shortcut" program Brian H¹© Computer Support 0 07-02-2003 01:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57