# String#split works w/ Regexp in IRB, not in rails

**URL:** <https://www.ruby-forum.com/t/string-split-works-w-regexp-in-irb-not-in-rails/92765>\
**Category:** Rails\
**Created:** [April 23, 2007, 11:40pm UTC](https://www.ruby-forum.com/t/string-split-works-w-regexp-in-irb-not-in-rails/92765 "2007-04-23T23:40:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![studbucket](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@studbucket](https://www.ruby-forum.com/u/studbucket)\
**Post date:** [April 23, 2007, 11:40pm UTC](https://www.ruby-forum.com/t/string-split-works-w-regexp-in-irb-not-in-rails/92765/1 "2007-04-23T23:40:25Z")

</div>

I’m trying to take a string in the format “word[field] and  
word2[field2]…”, etc. I have gotten this to work when splitting it in  
IRB using the following syntax:

tempstring.split(/[|]/)

which returns:

[“word”, “field”, “word2”, “field2”,…]

However, when I attempt to make the exact same call in a rails  
controller, I get the following error message on the line where  
String#split is called.

“can’t convert Regexp into String”

However, I don’t know why it should be trying to convert my Regexp into  
a string, when the API shows you can use strings or regular expressions.  
 → [class String - RDoc Documentation](http://www.ruby-doc.org/core/classes/String.html#M000818)

If you can’t figure it out from what I have given and need more code to  
look at, let me know.

Thanks,

JT

---

<div class="post-metadata">

**Author:** ![studbucket](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@studbucket](https://www.ruby-forum.com/u/studbucket)\
**Post date:** [April 23, 2007, 11:53pm UTC](https://www.ruby-forum.com/t/string-split-works-w-regexp-in-irb-not-in-rails/92765/2 "2007-04-23T23:53:04Z")

</div>

> I’m trying to take a string in the format “word[field] and  
> word2[field2]…”, etc. I have gotten this to work when splitting it in  
> IRB using the following syntax:
> 
> tempstring.split(/[|]/)

Maybe… tempstring.split(/[][]/)

I can’t think of a why irb likes yours, but not rails though…

---

<div class="post-metadata">

**Author:** ![studbucket](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@studbucket](https://www.ruby-forum.com/u/studbucket)\
**Post date:** [April 24, 2007, 12:33am UTC](https://www.ruby-forum.com/t/string-split-works-w-regexp-in-irb-not-in-rails/92765/3 "2007-04-24T00:33:37Z")

</div>

JT Kimbell wrote:

> I’m trying to take a string in the format “word[field] and  
> word2[field2]…”, etc. I have gotten this to work when splitting it in  
> IRB using the following syntax:
> 
> tempstring.split(/[|]/)
> 
> which returns:
> 
> [“word”, “field”, “word2”, “field2”,…]
> 
> However, when I attempt to make the exact same call in a rails  
> controller, I get the following error message on the line where  
> String#split is called.

How about you post the code?

Maybe tempstring happens to be a regexp for some reason.

Stepohan

---

<div class="post-metadata">

**Author:** ![studbucket](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@studbucket](https://www.ruby-forum.com/u/studbucket)\
**Post date:** [April 26, 2007, 6:20pm UTC](https://www.ruby-forum.com/t/string-split-works-w-regexp-in-irb-not-in-rails/92765/4 "2007-04-26T18:20:15Z")

</div>

Stephan W. wrote:

> How about you post the code?
> 
> Maybe tempstring happens to be a regexp for some reason.
> 
> Stepohan

Hi,

Here is the pertinent code:

* * *

## @querytemp = params[:content\_query] #The following code queries the pubmed site and gets their expanded query, while removing their markups. sh = Shell.new(); @querytemp2 = sh.system(‘getdetailquery’, @querytemp) @temparray = @querytemp2.split(/[|]/) #Split the string at brackets ( [and] )

getdetailquery is perl script that expands the query for me, and I can  
guarantee it works, I’ve tested it in irb, ruby programs, and by itself.

All of those variables are used for the first time in that code, so you  
are seeing them when they are ‘initialized’. The error is thrown on the  
line where the split method is being called.

One the line above it, I tried hard-coding the value ‘flu’ in for  
querytemp, and I still get the same error. It doesn’t like me passing a  
Regexp instead of a string for that ☹

Thanks,

JT
