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


#1

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


#2

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…


#3

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


#4

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

Thanks,

JT