[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to use split method in ruby

dare ruby

12/7/2007 11:22:00 AM

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

5 Answers

Morton Goldberg

12/7/2007 5:56:00 PM

0

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

NigamaX

12/7/2007 6:58:00 PM

0

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

NigamaX

12/7/2007 7:00:00 PM

0

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

dare ruby

12/8/2007 3:21:00 AM

0

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

dare ruby

12/8/2007 3:38:00 AM

0

hi nigama your code is working well in my application.

Thank you very much

regards,
martin
--
Posted via http://www.ruby-....