# How does this work?

**URL:** <https://www.ruby-forum.com/t/how-does-this-work/48703>\
**Category:** Ruby\
**Created:** [November 23, 2005, 4:46pm UTC](https://www.ruby-forum.com/t/how-does-this-work/48703 "2005-11-23T16:46:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gandalfmeister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/2acd7d/32.png) [@gandalfmeister](https://www.ruby-forum.com/u/gandalfmeister)\
**Post date:** [November 23, 2005, 4:46pm UTC](https://www.ruby-forum.com/t/how-does-this-work/48703/1 "2005-11-23T16:46:35Z")

</div>

I cam across a construct in the FXRuby examples which I’ve not seen  
before…  
… lots of constants…(about 47 in total I believe)  
ID\_INSERT\_FILE,  
ID\_EXTRACT\_FILE,  
ID\_WHEELADJUST,  
ID\_LAST = enum(FXMainWindow::ID\_LAST, 47)

and I’m assuming this generates a unique number for each piece of text,  
which is subsequently used in the rest of the code. How does it work?

Graham

---

<div class="post-metadata">

**Author:** ![gandalfmeister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/2acd7d/32.png) [@gandalfmeister](https://www.ruby-forum.com/u/gandalfmeister)\
**Post date:** [November 23, 2005, 4:58pm UTC](https://www.ruby-forum.com/t/how-does-this-work/48703/2 "2005-11-23T16:58:41Z")

</div>

On Nov 23, 2005, at 10:42 AM, Graham wrote:

> which is subsequently used in the rest of the code. How does it work?  
> Just a guess:

def enum( start, count )  
(start…start+count).to\_a  
end

a,b,c = enum( 101, 3 )  
p a,b,c  
#=\> 101  
#=\> 102  
#=\> 103

Except it looks like it’s using the last value to update a constant  
for future calls:

LAST\_ID = 101  
def enum( start, count )  
return (start…start+count).to\_a  
end

a,b,c,LAST\_ID = enum( LAST\_ID, 4 )  
p a,b,c  
#=\> 101  
#=\> 102  
#=\> 103

d,e,f,g,LAST\_ID = enum( LAST\_ID, 5 )  
p d,e,f,g  
#=\> 104  
#=\> 105  
#=\> 106  
#=\> 107

Note that the above (properly) generates warnings each time you  
reassign to the global value. IMO a proper ‘enum’ solution would keep  
track of that value internally.

---

<div class="post-metadata">

**Author:** ![gandalfmeister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/2acd7d/32.png) [@gandalfmeister](https://www.ruby-forum.com/u/gandalfmeister)\
**Post date:** [November 23, 2005, 7:19pm UTC](https://www.ruby-forum.com/t/how-does-this-work/48703/3 "2005-11-23T19:19:58Z")

</div>

Gavin K. wrote:

> Note that the above (properly) generates warnings each time you reassign  
> to the global value. IMO a proper ‘enum’ solution would keep track of  
> that value internally.  
> Doesn’t if you do

# Enumeration

def enum(start, count)  
(start…(start+count)).to\_a  
end  
as (start+count) is only evaluated once I assume.

Thanks for the clarification, once I realised it was just an FX specific  
method, and not some cool use of the Enumeration class it sort of fell  
into place.  
Cheers  
Graham

---

<div class="post-metadata">

**Author:** ![gandalfmeister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/2acd7d/32.png) [@gandalfmeister](https://www.ruby-forum.com/u/gandalfmeister)\
**Post date:** [November 23, 2005, 9:33pm UTC](https://www.ruby-forum.com/t/how-does-this-work/48703/4 "2005-11-23T21:33:25Z")

</div>

Gavin K. wrote:

> > and I’m assuming this generates a unique number for each piece of text,  
> > p a,b,c  
> > return (start…start+count).to\_a  
> > #=\> 104  
> > #=\> 105  
> > #=\> 106  
> > #=\> 107
> 
> Note that the above (properly) generates warnings each time you  
> reassign to the global value. IMO a proper ‘enum’ solution would keep  
> track of that value internally.

Like so?:

#!/usr/bin/env ruby

module Enum  
@@last = 0  
def enum(n)  
r = (@@last…(@@last+n-1)).to\_a  
@@last += n  
r  
end  
end

include Enum

ROBO\_LTHRUST,  
ROBO\_RTHRUST,  
ROBO\_FIRE = enum(3)

p ROBO\_LTHRUST, ROBO\_RTHRUST, ROBO\_FIRE
