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…
On Dec 7, 2007, at 6:22 AM, dare ruby wrote:
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.
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”)
kick
the
ball
kick
the
ball
MESSAGE 2
size
blue
It’s not very elegant, but maybe it will work for you.
Regards, Morton
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
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
It’s not very elegant, but maybe it will work for you.
Regards, Morton
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
hi nigama your code is working well in my application.
Thank you very much
regards,
martin