[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Comparing String with Symbol

Yukihiro Matsumoto

5/8/2008 3:33:00 PM

Hi,

In message "Re: Comparing String with Symbol"
on Fri, 9 May 2008 00:04:22 +0900, Roger Pack <rogerpack2005@gmail.com> writes:

|I usually just convert them over
|pos = pos.to_s
|> Maybe using:
|> if pos.to_s == 'first'
|> is the best way?

I'd rather use to_sym instead of to_s.

matz.

12 Answers

Roger Pack

5/8/2008 3:43:00 PM

0


> I'd rather use to_sym instead of to_s.
>
> matz.

I'd use either one except that [quoting Tim Harper]

once a symbol is allocated, it is never garbage collected.

This opens up a potential DOS attack: if you convert any user input to
symbols (via #to_sym or #intern), they could easily fill your symbol
space up and eat all of the available system memory.

We'd all be wiser than I was a few minutes ago to keep this present when
deciding whether to use symbols or strings:

http://blog.hasmanythrough.com/2008/4/19/symbols-are-not-pret...
--

or, in other words
loop { rand(10000000).to_sym }
# uses up memory in a hurry :)

-R
--
Posted via http://www.ruby-....

Michael Neumann

5/8/2008 4:07:00 PM

0

Roger Pack wrote:
>> I'd rather use to_sym instead of to_s.
>>
>> matz.
>
> I'd use either one except that [quoting Tim Harper]
>
> once a symbol is allocated, it is never garbage collected.
>
> This opens up a potential DOS attack: if you convert any user input to
> symbols (via #to_sym or #intern), they could easily fill your symbol
> space up and eat all of the available system memory.
>
> We'd all be wiser than I was a few minutes ago to keep this present when
> deciding whether to use symbols or strings:
>
> http://blog.hasmanythrough.com/2008/4/19/symbols-are-not-pret...
> --
>
> or, in other words
> loop { rand(10000000).to_sym }
> # uses up memory in a hurry :)

This one isn't better, despite being shorter ;-)

a = []; loop { a << 1 }

Or in general, "loop { }" is not a very good idea...

Couldn't resist ;-)

Regards,

Michael

Albert Schlef

5/8/2008 5:00:00 PM

0

Michael Neumann wrote:
> > # uses up memory in a hurry :)
>
> This one isn't better, despite being shorter ;-)
>
> a = []; loop { a << 1 }

A slight improvement is to change '1' to '1.0'. A Fixnum doesn't
allocate an object structure.
--
Posted via http://www.ruby-....

Michael Neumann

5/8/2008 5:18:00 PM

0

Albert Schlef wrote:
> Michael Neumann wrote:
>>> # uses up memory in a hurry :)
>> This one isn't better, despite being shorter ;-)
>>
>> a = []; loop { a << 1 }
>
> A slight improvement is to change '1' to '1.0'. A Fixnum doesn't
> allocate an object structure.

Yes, but it will run out space regardless :)

Regards,

Michael


Simon Krahnke

5/8/2008 5:44:00 PM

0

* Michael Neumann <mneumann@ntecs.de> (19:17) schrieb:

> Albert Schlef wrote:
>> Michael Neumann wrote:
>>>> # uses up memory in a hurry :)
>>> This one isn't better, despite being shorter ;-)
>>>
>>> a = []; loop { a << 1 }
>>
>> A slight improvement is to change '1' to '1.0'. A Fixnum doesn't
>> allocate an object structure.
>
> Yes, but it will run out space regardless :)

No, that depends on how much space is available.

mfg, simon .... l

Phillip Gawlowski

5/8/2008 6:17:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Simon Krahnke wrote:
| * Michael Neumann <mneumann@ntecs.de> (19:17) schrieb:
|
|> Albert Schlef wrote:
|>> Michael Neumann wrote:
|>>>> # uses up memory in a hurry :)
|>>> This one isn't better, despite being shorter ;-)
|>>>
|>>> a = []; loop { a << 1 }
|>> A slight improvement is to change '1' to '1.0'. A Fixnum doesn't
|>> allocate an object structure.
|> Yes, but it will run out space regardless :)
|
| No, that depends on how much space is available.

Space is a finite resource. Sooner or later, that code *will* consume
all available memory.

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.bl...

~ - You know you've been hacking too long when...
...you find out that you can't get to sleep, because you are, in fact,
the program you're designing, and can't run to completion as the lower
level routines haven't been coded yet. [This may be one of the
disadvantages of top-down design...]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkgjQ5EACgkQbtAgaoJTgL9KNACeO8LQ898C8c6mMFknW74JYkTQ
baYAoIjNS41z1+DU4Ap6gKRm8hwFjU4f
=GLfM
-----END PGP SIGNATURE-----

Iñaki Baz Castillo

5/10/2008 11:25:00 AM

0

El Jueves, 8 de Mayo de 2008, Roger Pack escribi=C3=B3:
> > I'd rather use to_sym instead of to_s.
> >
> > matz.
>
> I'd use either one except that [quoting Tim Harper]
>
> once a symbol is allocated, it is never garbage collected.
>
> This opens up a potential DOS attack: if you convert any user input to
> symbols (via #to_sym or #intern), they could easily fill your symbol
> space up and eat all of the available system memory.
>
> We'd all be wiser than I was a few minutes ago to keep this present when
> deciding whether to use symbols or strings:
>
> http://blog.hasmanythrough.com/2008/4/19/symbols-are-not-pret...

Thanks a lot for that explanation. Finally I'll use only symbols and force=
=20
method arguments to be symbols.
Parameters will only be created via coding andnot via HTTP request and so.

Thanks a lot.


=2D-=20
I=C3=B1aki Baz Castillo

Robert Dober

5/10/2008 12:21:00 PM

0

On Thu, May 8, 2008 at 8:16 PM, Phillip Gawlowski
<cmdjackryan@googlemail.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----

> Space is a finite resource. Sooner or later, that code *will* consume
> all available memory.
Although recent research seems to agree with you this seems to be a
bold statement nevertheless ;)
or do you mean memory, sorry could not resist.
Cheers
Robert

Evan David Light

5/10/2008 6:26:00 PM

0

In the PragProg Advanced Ruby Studio today, we were discussing
profiling tools with Chad Fowler and Dave Thomas. Neither are aware
of memory profiling tools for Ruby. Are there any mature memory
profiling tools out there already? A quick google search turns up
some old hits or small profiling apps.

Specifically, I was wondering about graphical memory profiling
tools. GraphViz seems an obvious approach.

Thanks,
Evan

Roger Pack

5/10/2008 6:39:00 PM

0

> Specifically, I was wondering about graphical memory profiling
> tools. GraphViz seems an obvious approach.

bleak house, ruby-prof has a memory extension and allows for
kcachegrind.
There's also a professional one out there that works in windows.

-R

>
> Thanks,
> Evan

--
Posted via http://www.ruby-....