[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

global generator of unique symbols

Thomas Hafner

1/31/2009 9:11:00 AM

Hello,

is there a global generator of unique symbols? See article
<r98456-qlb.ln1@faun.hafner.nl.eu.org> 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
8 Answers

pjb

1/31/2009 9:52:00 AM

0

Thomas Hafner <thomas@faun.hafner.nl.eu.org> writes:

> Hello,
>
> is there a global generator of unique symbols? See article
> <r98456-qlb.ln1@faun.hafner.nl.eu.org> 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.


--
__Pascal Bourguignon__

David A. Black

1/31/2009 11:53:00 AM

0

Hi --

On Sat, 31 Jan 2009, Thomas Hafner wrote:

> Hello,
>
> is there a global generator of unique symbols? See article
> <r98456-qlb.ln1@faun.hafner.nl.eu.org> 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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

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

David A. Black

1/31/2009 12:13:00 PM

0

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

> Thomas Hafner <thomas@faun.hafner.nl.eu.org> writes:
>
>> Hello,
>>
>> is there a global generator of unique symbols? See article
>> <r98456-qlb.ln1@faun.hafner.nl.eu.org> 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

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

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

Thomas Hafner

1/31/2009 1:07:00 PM

0

"David A. Black" <dblack@rubypal.com> wrote/schrieb <Pine.LNX.4.64.0901310705530.7683@rubypal.com>:

> 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

Matthew Moss

1/31/2009 2:08:00 PM

0

> But these two pairs don't:
>
> 1
> + 2
>
> 1 +
> 2

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

Gary Wright

1/31/2009 5:05:00 PM

0


On Jan 31, 2009, at 4:15 AM, Thomas Hafner wrote:
> is there a global generator of unique symbols? See article
> <r98456-qlb.ln1@faun.hafner.nl.eu.org> 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/...

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

UUIDs are discussed in RFC 4122: <http://www.ietf.org/rfc/rfc41...


Gary Wright




Thomas Hafner

1/31/2009 5:31:00 PM

0

Matthew Moss <matt@moss.name> wrote/schrieb <0854D1BD-5291-4896-8233-AECA6C6A3EF5@moss.name>:

>> 1
>> + 2
>>
>> 1 +
>> 2
>
> 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

Matthew Moss

1/31/2009 6:28:00 PM

0


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

> Matthew Moss <matt@moss.name> wrote/schrieb <0854D1BD-5291-4896-8233-AECA6C6A3EF5@moss.name
> >:
>
>>> 1
>>> + 2
>>>
>>> 1 +
>>> 2
>>
>> 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.

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