Seems so simple, but is this really the only way, or most directway?

Discussion in 'Ruby' started by John Pywtorak, Oct 18, 2006.

  1. 9 digits in a string "123456789", convert to individual Fixnums
    [1,2,3,4,5,6,7,8,9], or as below

    a, b, c, d, e, f, g, h, i = id.scan(/\d/).map(&:to_i)

    Simpler yet how about ["1", "2", ...]. Is scan the only direct way?

    I tried the "*" mechanism, no such luck, and tried array slicing again
    no luck. TIA
    --
    John Pywtorak
    Cal Poly State University
    ITS/ASAS
    805-756-5906
     
    John Pywtorak, Oct 18, 2006
    #1
    1. Advertising

  2. John Pywtorak

    Phrogz Guest

    Re: Seems so simple, but is this really the only way, or most direct way?

    John Pywtorak wrote:
    > 9 digits in a string "123456789", convert to individual Fixnums


    irb(main):001:0> s = "123456789"
    => "123456789"
    irb(main):002:0> s.split('').map{ |n| n.to_i }
    => [1, 2, 3, 4, 5, 6, 7, 8, 9]
     
    Phrogz, Oct 18, 2006
    #2
    1. Advertising

  3. John Pywtorak

    Mike Dvorkin Guest

    Re: Seems so simple, but is this really the only way, or most direct way?

    id.unpack('c' * id.size).map { |x| x-48 } # without regex and to_i
    conversions

    Mike Dvorkin
    http://www.rubywizards.com


    On Oct 17, 2006, at 5:36 PM, John Pywtorak wrote:

    > 9 digits in a string "123456789", convert to individual Fixnums
    > [1,2,3,4,5,6,7,8,9], or as below
    >
    > a, b, c, d, e, f, g, h, i = id.scan(/\d/).map(&:to_i)
    >
    > Simpler yet how about ["1", "2", ...]. Is scan the only direct way?
    >
    > I tried the "*" mechanism, no such luck, and tried array slicing
    > again no luck. TIA
    > --
    > John Pywtorak
    > Cal Poly State University
    > ITS/ASAS
    > 805-756-5906
    >
    >
    >
     
    Mike Dvorkin, Oct 18, 2006
    #3
  4. Re: Seems so simple, but is this really the only way, or most direct way?

    On 10/18/06, John Pywtorak <> wrote:
    > 9 digits in a string "123456789", convert to individual Fixnums
    > [1,2,3,4,5,6,7,8,9], or as below
    >
    > a, b, c, d, e, f, g, h, i = id.scan(/\d/).map(&:to_i)
    >
    > Simpler yet how about ["1", "2", ...]. Is scan the only direct way?
    >


    "123456789".split( '' ) #thats two single quotes. :)

    hth,
    -Harold
     
    Harold Hausman, Oct 18, 2006
    #4
  5. John Pywtorak

    Guest

    Re: Seems so simple, but is this really the only way,or most direct way?

    > 9 digits in a string "123456789", convert to individual Fixnums
    > [1,2,3,4,5,6,7,8,9], or as below
    >
    > a, b, c, d, e, f, g, h, i = id.scan(/\d/).map(&:to_i)
    >
    > Simpler yet how about ["1", "2", ...]. Is scan the only direct way?
    >
    > I tried the "*" mechanism, no such luck, and tried array slicing again
    > no luck. TIA


    (1..9).to_a

    ?

    I'm not quite sure what you are trying to do, you see :) Are you trying
    to actaully set variables? If you are, first I'd ask "why are you trying
    to set a range of instance variables". After that, if you're sure you do
    (and hell, "for fun" is an answer), then I'm sure that there is a method
    like instance_variable_set, only for manipulating local variables, but
    oddly, I don't seem able to find it at the moment...
     
    , Oct 18, 2006
    #5
    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. nc
    Replies:
    1
    Views:
    503
    nice.guy.nige
    Feb 3, 2005
  2. Jeannie
    Replies:
    15
    Views:
    893
    Jeannie
    Aug 30, 2005
  3. mike3
    Replies:
    4
    Views:
    281
    mike3
    Sep 1, 2007
  4. mike3
    Replies:
    36
    Views:
    1,369
    mike3
    Sep 20, 2007
  5. xkenneth
    Replies:
    8
    Views:
    342
    Bruno Desthuilliers
    Feb 6, 2008
Loading...

Share This Page