global generator of unique symbols

T

Thomas Hafner

Hello,

is there a global generator of unique symbols? See article
<[email protected]> why that can be needed. There I
have defined myself a generator, but that's no real solution: if
software is going to be put together from different libraries, only
one common generator should be used by all software parts to prevent
symbol clash.

With ``generator of unique symbols'' I mean: the first time since
program start it will generate any symbol. Any other time it will
generate a symbol that differs from all already generated symbols.

Regards
Thomas
 
P

Pascal J. Bourguignon

Thomas Hafner said:
Hello,

is there a global generator of unique symbols? See article
<[email protected]> why that can be needed. There I
have defined myself a generator, but that's no real solution: if
software is going to be put together from different libraries, only
one common generator should be used by all software parts to prevent
symbol clash.

With ``generator of unique symbols'' I mean: the first time since
program start it will generate any symbol. Any other time it will
generate a symbol that differs from all already generated symbols.

Regards
Thomas


(@@gensym_counter = 0)

(def gensym(base = "com_informatimago_ruby_g")
(@@gensym_counter = (@@gensym_counter + 1))
((base + (@@gensym_counter . to_s)) . to_sym)
end)

(gensym)
--> :com_informatimago_ruby_g5


You could use org_eu_nl_hafner_faun in your symbols, and bet nobody
else will use that prefix.
 
D

David A. Black

Hi --

Hello,

is there a global generator of unique symbols? See article
<[email protected]> why that can be needed. There I
have defined myself a generator, but that's no real solution: if
software is going to be put together from different libraries, only
one common generator should be used by all software parts to prevent
symbol clash.

With ``generator of unique symbols'' I mean: the first time since
program start it will generate any symbol. Any other time it will
generate a symbol that differs from all already generated symbols.

Based on a 2005 post from Jeremy Kemper, based in turn on a response
from Ryan Leavengood:

$unique_symbol = 'AAAAAAAA'
def generate_symbol
$unique_symbol.succ!.to_sym
end

As James Gray pointed out in the same thread, be careful with runaway
symbol generation, since symbols aren't garbage collected.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
 
D

David A. Black

(@@gensym_counter = 0)

(def gensym(base = "com_informatimago_ruby_g")
(@@gensym_counter = (@@gensym_counter + 1))
((base + (@@gensym_counter . to_s)) . to_sym)
end)

(gensym)
--> :com_informatimago_ruby_g5

I still don't get the fake Lisp thing. Why write cryptic, unidiomatic,
turgid Ruby code, when Ruby provides a clean, clear syntax? It's not
about understanding Ruby's precedence rules; I'm sure you know what

x += 1

means, and don't really need to write (x = (x + 1)) in order to
understand it, and likewise that you can figure out how to write:

(base + x.to_s).to_sym

without the added clutter.

I have an obscure feeling that you're trying to send us a message
about Lisp being more serious, and Ruby only being worthwhile when
it's made to look superficially like Lisp. But is that really doing
justice to either language? Is the chief lesson of studying Lisp
really that you should slap parentheses on as many expressions as
possible in other languages? It doesn't make sense to me.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
 
T

Thomas Hafner

David A. Black said:
I still don't get the fake Lisp thing. Why write cryptic, unidiomatic,
turgid Ruby code, when Ruby provides a clean, clear syntax?

Pascal's style seems to reduce errors by syntactical whitespace. These
two pair of lines evaluate to the same result:

(1
+ 2)

(1 +
2)

But these two pairs don't:

1
+ 2

1 +
2

Regards
Thomas
 
G

Gary Wright

is there a global generator of unique symbols? See article
<[email protected]> why that can be needed. There I
have defined myself a generator, but that's no real solution: if
software is going to be put together from different libraries, only
one common generator should be used by all software parts to prevent
symbol clash.

Perhaps generating a UUID and converting it to a symbol would work for
you?

<http://github.com/assaf/uuid/tree/master>

uuid = UUID.new
sym = uuid.generate:)compact).to_sym

UUIDs are discussed in RFC 4122: <http://www.ietf.org/rfc/rfc4122.txt>


Gary Wright
 
T

Thomas Hafner

Matthew Moss said:
Perhaps you should write 1 + 2 and be done with it.

Totally agreed :)

I meant ``1'' and ``2'' to be placeholders for more complex
expressions.

Regards
Thomas
 
M

Matthew Moss

Totally agreed :)

I meant ``1'' and ``2'' to be placeholders for more complex
expressions.

True; I understood what you were getting at... But I think, perhaps,
you were going a bit overboard in intent.

If you're going to break 1+2 over a line, then parenthesis (or
simplification of expressions, perhaps) becomes a necessity.

But something like:

(def foo
(stuff goes here
)
end)

This seems overdoing it. If you're constantly writing it in "correct"
form that doesn't require parenthesis, then it is wasted, adds
complexity, and begins to look like you have motives other than
protecting:
1
+ 2
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top