# Split not returning an array?

**URL:** <https://www.ruby-forum.com/t/split-not-returning-an-array/146794>\
**Category:** Ruby\
**Created:** [August 25, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/split-not-returning-an-array/146794 "2008-08-25T15:49:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@amanda](https://www.ruby-forum.com/u/amanda)\
**Post date:** [August 25, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/split-not-returning-an-array/146794/1 "2008-08-25T15:49:37Z")

</div>

Hi there, I start out with a string containing at least 1 ‘+’ separating  
words in it. I’m trying to get an array using str.split(’+’), so I can  
use each word to do something different.

So for example, I have string = “science+students”  
Now I want an array: [“science”, “students”]

Problem is, when I do string.split(’+’), I’m getting a single string  
with the ‘+’ removed, so if I do:

@cmds = @string.split(’+’)

for @item in @cmds  
puts 1  
end

‘1’ is only outputted once, and if I just print out @cmds, I get  
“science students” (I used this to see how many items were in the  
‘array’)

Can anyone explain to me what’s happening here? It looks like the split  
method is returning the original strings without the ‘+’ characters, and  
not an array as it should be…

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![amanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@amanda](https://www.ruby-forum.com/u/amanda)\
**Post date:** [August 25, 2008, 3:58pm UTC](https://www.ruby-forum.com/t/split-not-returning-an-array/146794/2 "2008-08-25T15:58:56Z")

</div>

On Mon, Aug 25, 2008 at 7:45 AM, Amanda … [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> @cmds = @string.split(‘+’)  
> method is returning the original strings without the ‘+’ characters, and  
> not an array as it should be…
> 
> ## Thanks for any help!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

split behaves exactly as documented for me. What is the full section of  
code where this is happening? Does:

string = “science+students”  
string.split(‘+’)

return [“science”, “students”]

in irb on your machine?

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![amanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@amanda](https://www.ruby-forum.com/u/amanda)\
**Post date:** [August 25, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/split-not-returning-an-array/146794/3 "2008-08-25T15:59:32Z")

</div>

2008/8/25 Amanda … [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Can anyone explain to me what’s happening here? It looks like the split  
> method is returning the original strings without the ‘+’ characters, and  
> not an array as it should be…

I have a feeling you’re dealing with data that has passed through  
CGI.unescape somewhere before you called split on it:

irb(main):006:0\> CGI.uescape(‘science+students’)  
=\> “science students”

Farrel

---

<div class="post-metadata">

**Author:** ![amanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@amanda](https://www.ruby-forum.com/u/amanda)\
**Post date:** [August 25, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/split-not-returning-an-array/146794/4 "2008-08-25T16:10:58Z")

</div>

Okay you’re all right, before the split it’s already “science students”  
… how can I stop that from happening?

I’m pulling it in through a URL for a calendar/events site. The URL  
determines what calendars to pull events from (ie science and students),  
so I have params[:cal] that’s supposed to store the string. Why is it  
unescaping it for me?

Should I just do a gsub!(’ ', ‘+’) maybe?

Thanks for helping so fast!

---

<div class="post-metadata">

**Author:** ![amanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@amanda](https://www.ruby-forum.com/u/amanda)\
**Post date:** [August 25, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/split-not-returning-an-array/146794/5 "2008-08-25T16:01:18Z")

</div>

On Mon, Aug 25, 2008 at 3:45 PM, Amanda … [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> @cmds = @string.split(‘+’)  
> method is returning the original strings without the ‘+’ characters, and  
> not an array as it should be…

Your code works perfectly, see below, so my only theory is that  
@string doesn’t have what you think it has.  
Can you print it before the split?

irb(main):021:0\> @string = “science+students”  
=\> “science+students”  
irb(main):022:0\> @cmds = @string.split(‘+’)  
=\> [“science”, “students”]  
irb(main):023:0\> for @item in @cmds  
irb(main):024:1\> puts 1  
irb(main):025:1\> end  
1  
1  
=\> [“science”, “students”]

Jesus.

---

<div class="post-metadata">

**Author:** ![amanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@amanda](https://www.ruby-forum.com/u/amanda)\
**Post date:** [August 25, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/split-not-returning-an-array/146794/6 "2008-08-25T16:28:17Z")

</div>

Glen H. wrote:

> On Mon, Aug 25, 2008 at 8:06 AM, Amanda … [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ## Thanks for helping so fast!
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> You could just split(’ ')
> 
> –  
> “Hey brother Christian with your high and mighty errand, Your actions  
> speak  
> so loud, I can’t hear a word you’re saying.”
> 
> -Greg Graffin (Bad Religion)

Problem with that is some of the calendars have spaces in their names  
(ie General Public) so I have it set up so that people have to put a ‘-’  
wherever there’s a space in a name, then I gsub(‘-’, ’ ‘) afterwards.  
I’m trying to do a gsub(’ ‘, ‘+’), and then the gsub(’-', ’ ').

---

<div class="post-metadata">

**Author:** ![amanda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/67e7ee/32.png) [@amanda](https://www.ruby-forum.com/u/amanda)\
**Post date:** [August 25, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/split-not-returning-an-array/146794/7 "2008-08-25T16:17:45Z")

</div>

On Mon, Aug 25, 2008 at 8:06 AM, Amanda … [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Thanks for helping so fast!
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

You could just split(’ ')

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)
