case when - symbols or strings?

M

Marc Heiler

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.
 
G

Gary Wright

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
 
F

F. Senault

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
 
R

Robert Klemme

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
 
J

Joel VanderWerf

Marc said:
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
 
S

Simon Krahnke

* Marc Heiler said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top