Comparing String with Symbol

Hi, losts of Ruby methods allow String or Symbol as parameter, for
example:

[“AAA”,“BBB”].send(‘first’)
=> “AAA”

[“AAA”,“BBB”].send(:first)
=> “AAA”

If I want to accespt String or Symbol, is there an easy wat to allow
both without doing something explicit as:
if pos == :first || pos == ‘first’

Maybe using:
if pos.to_s == ‘first’
is the best way?

I usually just convert them over
pos = pos.to_s

Maybe using:
if pos.to_s == ‘first’
is the best way?

Hi,

In message “Re: Comparing String with Symbol”
on Fri, 9 May 2008 00:04:22 +0900, Roger P.
[email protected] 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.

Hi,

In message “Re: Comparing String with Symbol”
on Fri, 9 May 2008 00:43:19 +0900, Roger P.
[email protected] writes:

|I’d use either one except that [quoting Tim H.]
|
|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.

That’s true but there are some other ways for potential DOS attack,
which I don’t disclose here. So you don’t have to worry too much.

Anyway, I have a plan to make Symbol garbage collected in 1.9 near
future.

          matz.

I’d rather use to_sym instead of to_s.

          matz.

I’d use either one except that [quoting Tim H.]

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:

has_many :through - Symbols are not pretty strings

or, in other words
loop { rand(10000000).to_sym }

uses up memory in a hurry :slight_smile:

-R

Roger P. wrote:

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:

has_many :through - Symbols are not pretty strings

or, in other words
loop { rand(10000000).to_sym }

uses up memory in a hurry :slight_smile:

This one isn’t better, despite being shorter :wink:

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

Or in general, “loop { }” is not a very good idea…

Couldn’t resist :wink:

Regards,

Michael

Michael N. wrote:

uses up memory in a hurry :slight_smile:

This one isn’t better, despite being shorter :wink:

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 :slight_smile:
No, that depends on how much space is available.

mfg, simon … l

Albert S. wrote:

Michael N. wrote:

uses up memory in a hurry :slight_smile:

This one isn’t better, despite being shorter :wink:

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 :slight_smile:

Regards,

Michael

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

Simon K. wrote:
| * Michael N. [email protected] (19:17) schrieb:
|
|> Albert S. wrote:
|>> Michael N. wrote:
|>>>> # uses up memory in a hurry :slight_smile:
|>>> This one isn’t better, despite being shorter :wink:
|>>>
|>>> 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 :slight_smile:
|
| 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 G.
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

~ - 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.mozdev.org

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

El Jueves, 8 de Mayo de 2008, Roger P. escribió:

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:

has_many :through - Symbols are not pretty strings

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

Thanks a lot.

In the PragProg Advanced Ruby Studio today, we were discussing
profiling tools with Chad F. and Dave T… 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

On Thu, May 8, 2008 at 8:16 PM, Phillip G.
[email protected] 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 :wink:
or do you mean memory, sorry could not resist.
Cheers
Robert

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

Evan David L. wrote:

Evan

What do you mean by “memory profiling?”

On May 10, 2008, at 11:26 , Evan David L. wrote:

In the PragProg Advanced Ruby Studio today, we were discussing

Please don’t hijack threads. Start a new mail thread like a good person.