# Mysql escape array

**URL:** <https://www.ruby-forum.com/t/mysql-escape-array/126952>\
**Category:** Ruby\
**Created:** [January 23, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/mysql-escape-array/126952 "2008-01-23T18:50:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JacobC2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbce88/32.png) [@JacobC2](https://www.ruby-forum.com/u/JacobC2)\
**Post date:** [January 23, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/mysql-escape-array/126952/1 "2008-01-23T18:50:45Z")

</div>

I am writing a program that reads a text file and then dumps it into a  
mysql database, the problem that I am having is that the array has  
illegal characters in it so I need to do an escape\_string or something  
on it. but how could I do this on an array?

---

<div class="post-metadata">

**Author:** ![JacobC2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbce88/32.png) [@JacobC2](https://www.ruby-forum.com/u/JacobC2)\
**Post date:** [January 23, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/mysql-escape-array/126952/2 "2008-01-23T19:34:05Z")

</div>

On Jan 23, 2008 11:49 AM, JacobC2 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am writing a program that reads a text file and then dumps it into a  
> mysql database, the problem that I am having is that the array has  
> illegal characters in it so I need to do an escape\_string or something  
> on it. but how could I do this on an array?

You probably should iterate. Look at Array#map!

Todd

---

<div class="post-metadata">

**Author:** ![JacobC2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbce88/32.png) [@JacobC2](https://www.ruby-forum.com/u/JacobC2)\
**Post date:** [January 23, 2008, 9:38pm UTC](https://www.ruby-forum.com/t/mysql-escape-array/126952/3 "2008-01-23T21:38:10Z")

</div>

Heya

On Jan 23, 2008 6:49 PM, JacobC2 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am writing a program that reads a text file and then dumps it into a  
> mysql database, the problem that I am having is that the array has  
> illegal characters in it so I need to do an escape\_string or something  
> on it. but how could I do this on an array?

You can use map! or collect! or their non-destructive brothers map and  
collect. collect/map apply a method to each element in the array and  
return a new array with the results.

# lines is some array

escaped = lines.map { |element| some\_mysql\_escape(element) }

---

<div class="post-metadata">

**Author:** ![JacobC2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbce88/32.png) [@JacobC2](https://www.ruby-forum.com/u/JacobC2)\
**Post date:** [January 24, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/mysql-escape-array/126952/4 "2008-01-24T21:25:53Z")

</div>

On Jan 23, 3:37 pm, Thomas W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> collect. collect/map apply a method to each element in the array and  
> return a new array with the results.
> 
> # lines is some array
> 
> escaped = lines.map { |element| some\_mysql\_escape(element) }

Thanks that work.
