I'm a newbie, just started programming Ruby about 1 month ago. Have lots
of experience with vb so the programming concepts are clear to me. I'm
building a script that is using the sample stream of the Twitter API and
insert it into MySQL for analysis. The problem is, that some of the
tweets have a ' in them. I want to replace them with '' so insert into
SQL is not a problem. I have written some piece of code trying to get
this to work, but the output replaces every letter with ''. This is the
code:
if line.include?"'"
result=""
teller=line.length-1
for i in 1..teller do
if line[i]="'"
result=result + "''"
else
result=result + line[i]
end
end
line=result
end
I'm a newbie, just started programming Ruby about 1 month ago. Have lots
of experience with vb so the programming concepts are clear to me. I'm
building a script that is using the sample stream of the Twitter API and
insert it into MySQL for analysis. The problem is, that some of the
tweets have a ' in them. I want to replace them with '' so insert into
SQL is not a problem. I have written some piece of code trying to get
this to work, but the output replaces every letter with ''. This is the
code:
You're probably going about this the wrong way - your database layer
should provide a quoting mechanism for you. For example, if you're using
the low-level mysql-ruby then use Mysql.quote(str); if you're using
something like ActiveRecord then that handles quoting for you.
in VB, '=' can means both assignment and equality, so the following does
what you expect:
if line[i]="'"
in ruby, you want '==' for equality (and '=' for assignment), so the above
line should be
···
if line[i]=="'"
On Mon, Jan 24, 2011 at 4:17 AM, Roland Klein overmeer < roland@kleinovermeer.nl> wrote:
Hello All,
I'm a newbie, just started programming Ruby about 1 month ago. Have lots
of experience with vb so the programming concepts are clear to me. I'm
building a script that is using the sample stream of the Twitter API and
insert it into MySQL for analysis. The problem is, that some of the
tweets have a ' in them. I want to replace them with '' so insert into
SQL is not a problem. I have written some piece of code trying to get
this to work, but the output replaces every letter with ''. This is the
code:
if line.include?"'"
result=""
teller=line.length-1
for i in 1..teller do
if line[i]="'"
result=result + "''"
else
result=result + line[i]
end
end
line=result
end