how to use split method in ruby

D

dare ruby

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....
 
M

Morton Goldberg

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
 
S

Scott Sweeney

dare said:
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
 
S

Scott Sweeney

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
 
D

dare ruby

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

regards
Martin
 
D

dare ruby

hi nigama your code is working well in my application.

Thank you very much

regards,
martin
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top