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ñaki Baz Castillo
<ibc@aliax.net>

I usually just convert them over
pos = pos.to_s

···

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

--
Posted via http://www.ruby-forum.com/\.

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.

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-pretty-strings

···

--

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

-R
--
Posted via http://www.ruby-forum.com/\.

Hi,

···

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

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.

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.

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:

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

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.

···

El Jueves, 8 de Mayo de 2008, Roger Pack escribió:

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

has_many :through - Symbols are not pretty strings

--
Iñaki Baz Castillo

Michael Neumann 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.

···

--
Posted via http://www.ruby-forum.com/\.

Albert Schlef wrote:

Michael Neumann 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

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

Albert Schlef wrote:

Michael Neumann 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

Simon Krahnke wrote:

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

Albert Schlef wrote:

Michael Neumann 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 Gawlowski
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...]

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

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

···

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.

  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-forum.com/\.

Evan David Light wrote:

    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

What do you mean by "memory profiling?"

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

···

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

  In the PragProg Advanced Ruby Studio today, we were discussing