# How to use split method in ruby

**URL:** <https://www.ruby-forum.com/t/how-to-use-split-method-in-ruby/122414>\
**Category:** Ruby\
**Created:** [December 7, 2007, 12:22pm UTC](https://www.ruby-forum.com/t/how-to-use-split-method-in-ruby/122414 "2007-12-07T12:22:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 7, 2007, 12:22pm UTC](https://www.ruby-forum.com/t/how-to-use-split-method-in-ruby/122414/1 "2007-12-07T12:22:07Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 7, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/how-to-use-split-method-in-ruby/122414/2 "2007-12-07T18:56:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 7, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/how-to-use-split-method-in-ruby/122414/3 "2007-12-07T19:58:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 8, 2007, 4:20am UTC](https://www.ruby-forum.com/t/how-to-use-split-method-in-ruby/122414/4 "2007-12-08T04:20:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 7, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/how-to-use-split-method-in-ruby/122414/5 "2007-12-07T20:00:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [December 8, 2007, 4:34am UTC](https://www.ruby-forum.com/t/how-to-use-split-method-in-ruby/122414/6 "2007-12-08T04:34:20Z")

</div>

hi nigama your code is working well in my application.

Thank you very much

regards,  
martin
