case when - symbols or strings?

Discussion in 'Ruby' started by Marc Heiler, Mar 13, 2009.

  1. Marc Heiler

    Marc Heiler Guest

    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-forum.com/.
     
    Marc Heiler, Mar 13, 2009
    #1
    1. Advertising

  2. Marc Heiler

    Gary Wright Guest

    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
     
    Gary Wright, Mar 13, 2009
    #2
    1. Advertising

  3. Marc Heiler

    F. Senault Guest

    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)
     
    F. Senault, Mar 13, 2009
    #3
  4. 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
     
    Robert Klemme, Mar 13, 2009
    #4
  5. 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
     
    Joel VanderWerf, Mar 13, 2009
    #5
  6. * Marc Heiler <> (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
     
    Simon Krahnke, Mar 15, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
  2. anonym
    Replies:
    1
    Views:
    1,022
    Knute Johnson
    Jan 15, 2009
  3. Daniel Berger
    Replies:
    2
    Views:
    427
    Park Heesob
    Nov 28, 2003
  4. Joe Van Dyk

    symbols vs strings vs ?

    Joe Van Dyk, Feb 3, 2005, in forum: Ruby
    Replies:
    2
    Views:
    88
    Joe Van Dyk
    Feb 3, 2005
  5. Mark Volkmann

    symbols vs. strings

    Mark Volkmann, Aug 26, 2005, in forum: Ruby
    Replies:
    0
    Views:
    98
    Mark Volkmann
    Aug 26, 2005
Loading...

Share This Page