# Split string

**URL:** <https://www.ruby-forum.com/t/split-string/129634>\
**Category:** Ruby\
**Created:** [February 20, 2008, 7:52am UTC](https://www.ruby-forum.com/t/split-string/129634 "2008-02-20T07:52:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 20, 2008, 7:52am UTC](https://www.ruby-forum.com/t/split-string/129634/1 "2008-02-20T07:52:18Z")

</div>

Hi

I have @selected\_ciid\_and\_asso\_types ( its content is  
3:43,2:65,3:50, )  
How can i split it like

3 43  
2 65  
3 50

These are actually two columns of a table…If i get it like above I can  
directly save it to database

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 20, 2008, 8:02am UTC](https://www.ruby-forum.com/t/split-string/129634/2 "2008-02-20T08:02:22Z")

</div>

I’m not sure if i really understand your question but if 3:43,2:65,3:50  
is  
actually a string i.e. “3:43,2:65,3:50”

then “3:43,2:65,3:50”.gsub(’:’,’ ‘).split(’,’) will return an array  
containing:

[“3 43”, “2 65”, “3 50”]

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 20, 2008, 8:11am UTC](https://www.ruby-forum.com/t/split-string/129634/3 "2008-02-20T08:11:47Z")

</div>

Hi,

On Feb 20, 2008 6:00 PM, Dan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m not sure if i really understand your question but if 3:43,2:65,3:50 is  
> actually a string i.e. “3:43,2:65,3:50”
> 
> then “3:43,2:65,3:50”.gsub(‘:’,’ ‘).split(’,') will return an array  
> containing:
> 
> [“3 43”, “2 65”, “3 50”]

If they’re two columns of a database, then this will probably be more  
helpful:

rows = @selected\_ciid\_and\_asso\_types.split(‘,’).map {|e| e.split(‘:’)}  
=\> [[“3”, “43”], [“2”, “65”], [“3”, “50”]]

You could also change the last bit to e.split(‘:’).map {|s| s.to\_i} if  
you  
wanted [[3, 43], [2, 65], [3, 50]] instead.

Arlen

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 20, 2008, 8:39am UTC](https://www.ruby-forum.com/t/split-string/129634/4 "2008-02-20T08:39:52Z")

</div>

Hi  
This is working…But what I need is like  
ci\_id service\_desk\_ci\_id

3 43  
2 65  
3 50

Then with in a for loop

@obj.ci\_id=3  
@obj.service\_desk\_ci\_id=43

@obj.ci\_id=2  
@obj.service\_desk\_ci\_id=65

@obj.ci\_id=3  
@obj.service\_desk\_ci\_id=50

How can i do this ?

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 20, 2008, 9:00am UTC](https://www.ruby-forum.com/t/split-string/129634/5 "2008-02-20T09:00:46Z")

</div>

Hi  
Thanks a lot…It is working…To get more understanding of all these  
can u suggest me a good book or online tutorial…I am a beginner to  
rails

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 20, 2008, 8:46am UTC](https://www.ruby-forum.com/t/split-string/129634/6 "2008-02-20T08:46:34Z")

</div>

Hi,

rows = @selected\_ciid\_and\_asso\_types.split(’,’).map {|e|  
e.split(’:’).map  
{|s| s.to\_i}}  
rows.each do |r|  
obj = make\_obj\_somehow()  
obj.ci\_id, obj.service\_desk\_ci\_id = r  
end

Something like this?

Arlen

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 20, 2008, 9:06am UTC](https://www.ruby-forum.com/t/split-string/129634/7 "2008-02-20T09:06:41Z")

</div>

Hi there,

For Rails, try Agile Web D. with Rails: A Pragmatic Guide - it  
got  
me off the ground in Rails, but you’ll still need to put in effort to  
grasp  
the situation clearly. Learning the ins and outs of Ruby is important.  
🙂

Arlen

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 20, 2008, 9:09am UTC](https://www.ruby-forum.com/t/split-string/129634/8 "2008-02-20T09:09:33Z")

</div>

Thanks again .For ruby learning may you suggest a good book

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [February 20, 2008, 9:45am UTC](https://www.ruby-forum.com/t/split-string/129634/9 "2008-02-20T09:45:36Z")

</div>

Sijo Kg wrote:

> Thanks again .For ruby learning may you suggest a good book
> 
> Sijo

Consider the new book:

The Ruby P.ming Language

One of the authors, David Flanagan, previously wrote ‘the’ book for  
Javascript programming, so I would expect this book to be top notch.
