Forum: Ruby global generator of unique symbols

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2cb736dc6ab6b986406912b22dff9ea7?d=identicon&s=25 Thomas Hafner (Guest)
on 2009-01-31 10:15
(Received via mailing list)
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
9e2504e0b74e5384af09ce8a660afac4?d=identicon&s=25 Pascal J. Bourguignon (Guest)
on 2009-01-31 10:55
(Received via mailing list)
Thomas Hafner <thomas@faun.hafner.nl.eu.org> 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.
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-01-31 12:53
(Received via mailing list)
Hi --

On Sat, 31 Jan 2009, Thomas Hafner 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 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!
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-01-31 13:17
(Received via mailing list)
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 (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
2cb736dc6ab6b986406912b22dff9ea7?d=identicon&s=25 Thomas Hafner (Guest)
on 2009-01-31 14:05
(Received via mailing list)
"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
A61ecce13ed142622f24a5ca3a123922?d=identicon&s=25 Matthew Moss (Guest)
on 2009-01-31 15:08
(Received via mailing list)
> But these two pairs don't:
>
>  1
>  + 2
>
>  1 +
>  2

Perhaps you should write 1 + 2 and be done with it.
47aff267a58c012d222fd4d74f6beb54?d=identicon&s=25 Dominik Honnef (Guest)
on 2009-01-31 17:42
(Received via mailing list)
> 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
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2009-01-31 18:05
(Received via mailing list)
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/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
2cb736dc6ab6b986406912b22dff9ea7?d=identicon&s=25 Thomas Hafner (Guest)
on 2009-01-31 18:35
(Received via mailing list)
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
A61ecce13ed142622f24a5ca3a123922?d=identicon&s=25 Matthew Moss (Guest)
on 2009-01-31 19:28
(Received via mailing list)
On Jan 31, 2009, at 11:35 AM, Thomas Hafner wrote:

>
> 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
This topic is locked and can not be replied to.