Global generator of unique symbols

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

Thomas H. [email protected] writes:

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.

Hi –

On Sat, 31 Jan 2009, Thomas H. wrote:

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 K., based in turn on a response
from Ryan L.:

$unique_symbol = ‘AAAAAAAA’
def generate_symbol
$unique_symbol.succ!.to_sym
end

As James G. 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 (The Well-Grounded Rubyist)

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

On Sat, 31 Jan 2009, Pascal J. Bourguignon wrote:

(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 (The Well-Grounded Rubyist)

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

But these two pairs don’t:

1

  • 2

1 +
2

Perhaps you should write 1 + 2 and be done with it.

“David A. Black” [email protected] wrote/schrieb
[email protected]:

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

(1 +
2)

But these two pairs don’t:

1

  • 2

1 +
2

Regards
Thomas

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.

+1

On Jan 31, 2009, at 4:15 AM, Thomas H. wrote:

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 W.

Matthew M. [email protected] wrote/schrieb
[email protected]:

1

  • 2

1 +
2

Perhaps you should write 1 + 2 and be done with it.

Totally agreed :slight_smile:

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

Regards
Thomas

On Jan 31, 2009, at 11:35 AM, Thomas H. wrote:

Totally agreed :slight_smile:

I meant 1'' and2’’ 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