Results 1 to 10 of 10

Thread: Converting a String to Char Array in Ruby

  1. #1
    Join Date
    Feb 2007
    Beans
    Hidden!
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Converting a String to Char Array in Ruby

    I'm just getting started with Ruby, and I was wondering whether there was a simple method to convert a string into a character array. In Java you can do something like:

    Code:
    String word = "tomato";
    word.toCharArray();
    Is there a similar method in Ruby? If not, could someone show me how to do it? I'd appreciate the help.

  2. #2
    Join Date
    Jul 2005
    Location
    Ontario, Canada
    Beans
    366
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Converting a String to Char Array in Ruby

    In Ruby, at least in the current version, there are no chars. There are only strings.

    However, you can split up a string in to an Array of single character strings.
    There are several ways you could do this.

    For example (an irb session):
    Code:
    $ irb
    irb(main):001:0> "Hello, world!".split(//)
    => ["H", "e", "l", "l", "o", ",", " ", "w", "o", "r", "l", "d", "!"]
    irb(main):002:0> "Hello, world!".scan(/./)
    => ["H", "e", "l", "l", "o", ",", " ", "w", "o", "r", "l", "d", "!"]

  3. #3
    Join Date
    Jun 2005
    Location
    Chico CA
    Beans
    2

    Re: Converting a String to Char Array in Ruby

    FYI a quick little test I did showed that the scan method above is 1.6 times faster than the split.

  4. #4
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Converting a String to Char Array in Ruby

    Quote Originally Posted by hypexr View Post
    FYI a quick little test I did showed that the scan method above is 1.6 times faster than the split.
    Unfortunately, your post wasn't all that fast.


    *looks at date*
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  5. #5
    Join Date
    Apr 2008
    Location
    Germany
    Beans
    194

    Re: Converting a String to Char Array in Ruby

    I know the thread is ages old, but maybe this will help someone.
    You can also do this:

    word = "tomato"
    word.each_byte{|b| puts b.chr}

    This would output:
    t
    o
    m
    a
    t
    o

    The method each byte, takes a string apart byte by byte, returning the decimal value for the character at each index location.
    You can then do what you want with it, for example convert it back into characters using the 'chr' method, or stick it into an array, or whatever.
    Last edited by Jim_in_Germany; October 14th, 2009 at 07:57 PM.

  6. #6
    Join Date
    Jan 2010
    Location
    Belgrade, Serbia
    Beans
    1

    Re: Converting a String to Char Array in Ruby

    Quote Originally Posted by Jim_in_Germany View Post
    I know the thread is ages old, but maybe this will help someone.
    You can also do this:

    word = "tomato"
    word.each_byte{|b| puts b.chr}

    This would output:
    t
    o
    m
    a
    t
    o

    The method each byte, takes a string apart byte by byte, returning the decimal value for the character at each index location.
    You can then do what you want with it, for example convert it back into characters using the 'chr' method, or stick it into an array, or whatever.
    That works only for ASCII characters. Since Ruby strings are unicode, this solution is incomplete (would not work for all strings)

  7. #7
    Join Date
    Mar 2010
    Beans
    2

    Re: Converting a String to Char Array in Ruby

    We can also do this:
    Code:
    word = "Nice dog"
    word.chars.to_a
    => ["N", "i", "c", "e", " ", "d", "o", "g"]

  8. #8
    Join Date
    Jan 2010
    Beans
    57

    Re: Converting a String to Char Array in Ruby

    Quote Originally Posted by bvsatyaram View Post
    We can also do this:
    Code:
    word = "Nice dog"
    word.chars.to_a
    => ["N", "i", "c", "e", " ", "d", "o", "g"]
    ...this looks alot cleaner, but didn't work for me!

    Code:
    \sandBox.rb:2: undefined method `chars' for "Nice dog":String (NoMethodError)

  9. #9
    Join Date
    Mar 2010
    Beans
    2

    Re: Converting a String to Char Array in Ruby

    May be due to different ruby version.
    I have ruby -v 1.8.7 installed on my box.

  10. #10
    Join Date
    Jan 2010
    Beans
    57

    Re: Converting a String to Char Array in Ruby

    ...u'r right, I had v 1.8.6 on one system, and it was failing. Another laptop I have has 1.8.7, and I can use .chars there...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •