Forum: Ruby case when - symbols or strings?

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.
Robert H. (Guest)
on 2009-03-13 14:08
Picture a small method which accepts one argument. Inside this method is
simply a case/when menu, nothing else. The question I now have is ... -
should the argument be coerced to string or symbol?


case argument.to_sym

  vs

case argument.to_s

The problem is that I usually work with strings. In fact, to simplify my
life, I begin to believe it would be easier for me to throw symbols away
entirely whenever I have a case/when structure. (I rarely use symbols at
all anyway.)

Looking at other people's ruby code they rarely seem to use neither
strings nor symbols - most of the time they appear to use 'when
/some_regex/' or 'when Array'.

Or something peculiar like:
  case value
     when ::Hash

Which I am not even sure what it does...

Noone seems to use :symbols at all inside there, so it seems that it
would be simpler to not use symbols inside case/when structures as well.
Gary W. (Guest)
on 2009-03-13 14:56
(Received via mailing list)
On Mar 13, 2009, at 8:06 AM, Marc H. wrote:

> Picture a small method which accepts one argument. Inside this
> method is
> simply a case/when menu, nothing else. The question I now have
> is ... -
> should the argument be coerced to string or symbol?
> case argument.to_sym
>   vs
> case argument.to_s


Hard to generalize but if you coerce to a string
you'll create a string that can be garbage collected.
If you coerce to symbol you create a new symbol that
can't be garbage collected.

If your argument is coming from some external source
(e.g. an HTML form) and isn't constrained in any way,
then coercing to a symbol will introduce a small
'memory leak' in your program every time the case
statement is executed.

Gary W.
F. Senault (Guest)
on 2009-03-13 15:36
(Received via mailing list)
Le 13 mars à 13:06, Marc H. a écrit :

> Or something peculiar like:
>   case value
>      when ::Hash
>
> Which I am not even sure what it does...

It checks if the value is a member of the toplevel class Hash or one of
it's descendants - the same as 'value.is_a? Hash'.

If you are in a module and decide to create a Hash class, in the code of
your module, Hash will reference your own class.  To access the toplevel
(and builtin in this case) Hash, you need to use ::Hash.

Fred
Robert K. (Guest)
on 2009-03-13 19:30
(Received via mailing list)
On 13.03.2009 13:51, Gary W. wrote:
> On Mar 13, 2009, at 8:06 AM, Marc H. wrote:
>
>> Picture a small method which accepts one argument. Inside this
>> method is
>> simply a case/when menu, nothing else. The question I now have
>> is ... -
>> should the argument be coerced to string or symbol?
>> case argument.to_sym
>>   vs
>> case argument.to_s

OP: The question is, where does "argument" come from and what do you
want to do with this?

> Hard to generalize but if you coerce to a string
> you'll create a string that can be garbage collected.
> If you coerce to symbol you create a new symbol that
> can't be garbage collected.
>
> If your argument is coming from some external source
> (e.g. an HTML form) and isn't constrained in any way,
> then coercing to a symbol will introduce a small
> 'memory leak' in your program every time the case
> statement is executed.

Not necessarily: it depends on the input.

Kind regards

  robert
Joel VanderWerf (Guest)
on 2009-03-13 20:05
(Received via mailing list)
Marc H. wrote:
> Picture a small method which accepts one argument. Inside this method is
> simply a case/when menu, nothing else. The question I now have is ... -
> should the argument be coerced to string or symbol?

FWIW, in ruby 1.9 you don't have to make that choice:

$ irb19
irb(main):001:0> /foo/ === :foo
=> true
irb(main):002:0> case :foo; when /foo/; puts "FOO"; end
FOO
=> nil
Simon K. (Guest)
on 2009-03-15 21:05
(Received via mailing list)
* Marc H. <removed_email_address@domain.invalid> (2009-03-13) schrieb:

> Picture a small method which accepts one argument. Inside this method is
> simply a case/when menu, nothing else. The question I now have is ... -
> should the argument be coerced to string or symbol?

What is that supposed to do?

Deciding what to do based on some argument passed in is no good idea.
That might be, why you don't find it that often.

The exception of course is parsing. But there might be alternatives to
case-when, such as Hash-Lookups.

For Strings vs. Symbols: User supplied usually is in a String. When
programmer supplied data comes in small quantities Symbols are usually
more convenient.

mfg,                      simon .... l
This topic is locked and can not be replied to.