[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

case when - symbols or strings?

Marc Heiler

3/13/2009 12:06:00 PM

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.
--
Posted via http://www.ruby-....

5 Answers

Gary Wright

3/13/2009 12:51:00 PM

0


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




F. Senault

3/13/2009 1:34:00 PM

0

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
--
I don't know who you are, But you seem very nice
So will you talk to me?
Shall I tell you a story, Shall I tell you a dream?
They think I'm crazy. (K's Choice, Everything for free)

Robert Klemme

3/13/2009 5:28:00 PM

0

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


--
remember.guy do |as, often| as.you_can - without end

Joel VanderWerf

3/13/2009 6:03:00 PM

0

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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Simon Krahnke

3/15/2009 6:41:00 PM

0

* 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