how to use split method in ruby

Discussion in 'Ruby' started by dare ruby, Dec 7, 2007.

  1. dare ruby

    dare ruby Guest

    hi friends,

    i have created a onject for string class, like

    @news = String.new

    and added contents using

    @news >> somevalue

    "somevalue" includes some alphabets, numbers, even whitespaces, special
    characters.

    now i need to seperate @news based on whitespace like

    if @news contains 'kick the ball"
    i need out put as, like

    kick
    the
    ball
    same if @ news contains 'kick the="size" ball="blue""

    i need to split and output should be like

    kick
    the
    ball
    Some message : size
    blue

    how to solve this problem using ruby?

    Thanks in advance....
    --
    Posted via http://www.ruby-forum.com/.
    dare ruby, Dec 7, 2007
    #1
    1. Advertising

  2. On Dec 7, 2007, at 6:22 AM, dare ruby wrote:

    > if @news contains 'kick the ball"
    > i need out put as, like
    >
    > kick
    > the
    > ball
    > same if @ news contains 'kick the="size" ball="blue""
    >
    > i need to split and output should be like
    >
    > kick
    > the
    > ball
    > Some message : size
    > blue



    I'm not sure I understand your question, but here is a guess on how
    you might get the output you want.

    <code>
    str1 = "kick the ball"
    str2 = 'kick the="size" ball="blue"'

    def my_split(str, msg)
    str = str.gsub(/=\"/, ' #').split(/(?:\"\s*)|\s/)
    words, more = str.partition { |token| token[0] != ?# }
    unless more.empty?
    words << msg
    words << more.map { |token| token.delete("#") }
    end
    words
    end

    puts my_split(str1, "MESSAGE 1")
    puts
    puts my_split(str2, "MESSAGE 2")
    </code>

    <results>
    kick
    the
    ball

    kick
    the
    ball
    MESSAGE 2
    size
    blue
    </results>

    It's not very elegant, but maybe it will work for you.

    Regards, Morton
    Morton Goldberg, Dec 7, 2007
    #2
    1. Advertising

  3. dare ruby wrote:
    > hi friends,
    >
    > i have created a onject for string class, like
    >
    > @news = String.new
    >
    > and added contents using
    >
    > @news >> somevalue
    >
    > "somevalue" includes some alphabets, numbers, even whitespaces, special
    > characters.
    >
    > now i need to seperate @news based on whitespace like
    >
    > if @news contains 'kick the ball"
    > i need out put as, like
    >
    > kick
    > the
    > ball
    > same if @ news contains 'kick the="size" ball="blue""
    >
    > i need to split and output should be like
    >
    > kick
    > the
    > ball
    > Some message : size
    > blue
    >
    > how to solve this problem using ruby?
    >
    > Thanks in advance....



    Well, I'm still very new to Ruby, but I came across something similar
    that I needed to do already. Maybe it can help you. If you do the
    following:

    news = 'kick the="size" ball="blue"'
    news_word_array = news.gsub(/[\"=]/ ' ').split

    It should return all of the words you are asking for in an array and
    then you can call and arrange that data in whatever way you see fit.

    Nigama
    --
    Posted via http://www.ruby-forum.com/.
    Scott Sweeney, Dec 7, 2007
    #3
  4. >> Thanks in advance....
    >
    >
    > Well, I'm still very new to Ruby, but I came across something similar
    > that I needed to do already. Maybe it can help you. If you do the
    > following:
    >
    > news = 'kick the="size" ball="blue"'
    > news_word_array = news.gsub(/[\"=]/, ' ').split
    >
    > It should return all of the words you are asking for in an array and
    > then you can call and arrange that data in whatever way you see fit.
    >
    > Nigama


    Gah! There's no edit on this forum!!! Oh, right it's also a mailing
    list. :(

    Anyway, sorry if I'm sending too many messages, but I made a small error
    in my code above and wanted to fix it.

    news = 'kick the="size" ball="blue"'
    news_word_array = news.gsub(/[\"=]/, ' ').split
    --
    Posted via http://www.ruby-forum.com/.
    Scott Sweeney, Dec 7, 2007
    #4
  5. dare ruby

    dare ruby Guest

    Thank you morton for spending your valuable time for me. its really have
    been very useful for me.

    regards
    Martin

    >
    > kick
    > the
    > ball
    > MESSAGE 2
    > size
    > blue
    > </results>
    >
    > It's not very elegant, but maybe it will work for you.
    >
    > Regards, Morton

    --
    Posted via http://www.ruby-forum.com/.
    dare ruby, Dec 8, 2007
    #5
  6. dare ruby

    dare ruby Guest

    hi nigama your code is working well in my application.

    Thank you very much

    regards,
    martin
    --
    Posted via http://www.ruby-forum.com/.
    dare ruby, Dec 8, 2007
    #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. Replies:
    2
    Views:
    450
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    684
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    201
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    221
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    582
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page