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.
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2009-03-13 13: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.
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2009-03-13 13:56
(Received via mailing list)
On Mar 13, 2009, at 8:06 AM, Marc Heiler 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 Wright
B57c5af36f5c1f33243dd8b2dd9043b1?d=identicon&s=25 F. Senault (Guest)
on 2009-03-13 14:36
(Received via mailing list)
Le 13 mars à 13:06, Marc Heiler 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-03-13 18:30
(Received via mailing list)
On 13.03.2009 13:51, Gary Wright wrote:
> On Mar 13, 2009, at 8:06 AM, Marc Heiler 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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-03-13 19:05
(Received via mailing list)
Marc Heiler 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
1d53b088a989e069b94597c282eebbbc?d=identicon&s=25 Simon Krahnke (Guest)
on 2009-03-15 20:05
(Received via mailing list)
* Marc Heiler <shevegen@linuxmail.org> (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.