# Ruby Array to Java Script Array

**URL:** <https://rubytalk.org/t/ruby-array-to-java-script-array/57044>\
**Category:** ruby-talk\
**Created:** [18 January 2010 06:24 UTC](https://rubytalk.org/t/ruby-array-to-java-script-array/57044 "2010-01-18T06:24:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![THAKUR\_PRASHANT\_SING](https://avatars.discourse-cdn.com/v4/letter/t/ecccb3/32.png) [@THAKUR\_PRASHANT\_SING](https://rubytalk.org/u/THAKUR_PRASHANT_SING)\
**Post date:** [18 January 2010 06:24 UTC](https://rubytalk.org/t/ruby-array-to-java-script-array/57044/1 "2010-01-18T06:24:54Z")

</div>

Hi,

How can we use an array created in Ruby in Java script ?

Regards,  
Prashant

---

<div class="post-metadata">

**Author:** ![Yuya\_Takeyama](https://avatars.discourse-cdn.com/v4/letter/y/a4c791/32.png) [@Yuya\_Takeyama](https://rubytalk.org/u/Yuya_Takeyama)\
**Post date:** [18 January 2010 08:08 UTC](https://rubytalk.org/t/ruby-array-to-java-script-array/57044/2 "2010-01-18T08:08:16Z")

</div>

(2010/01/18 15:24), THAKUR PRASHANT SINGH wrote:

> Hi,
> 
> How can we use an array created in Ruby in Java script ?
> 
> Regards,  
> Prashant

Hi.

JSON library makes it possible.

For example:  
[http://flori.github.com/json/](http://flori.github.com/json/)

Usage:  
puts %w!foo bar baz!.to\_json

---

<div class="post-metadata">

**Author:** ![THAKUR\_PRASHANT\_SING](https://avatars.discourse-cdn.com/v4/letter/t/ecccb3/32.png) [@THAKUR\_PRASHANT\_SING](https://rubytalk.org/u/THAKUR_PRASHANT_SING)\
**Post date:** [18 January 2010 09:14 UTC](https://rubytalk.org/t/ruby-array-to-java-script-array/57044/3 "2010-01-18T09:14:00Z")

</div>

I tried using json but I am doing some mistake because of which I am not able to get value properly  
I have such code in ruby file  
&nbsp;&nbsp;&nbsp;&nbsp;@files = Dir.glob("\*.txt").to\_json  
In view file  
&nbsp;&nbsp;var files= ('\<%= h @kmlfiles -%\>');  
When I see the alert I get a complete string containing list of all files  
When I run this alert I see one character printed at a time iso a single file name.  
&nbsp;&nbsp;for(i=0;i\<3;i++)  
&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(files[i]);  
&nbsp;&nbsp;&nbsp;&nbsp;}

I am making some mistake in using json but could not figure out what.  
Can you please help ?

> **···**
>
> -----Original Message-----  
> From: 竹山 雄也 [mailto:sign.of.the.wolf.pentagram@gmail.com]  
> Sent: Monday, January 18, 2010 1:38 PM  
> To: ruby-talk ML  
> Subject: Re: Ruby Array to Java Script Array
> 
> (2010/01/18 15:24), THAKUR PRASHANT SINGH wrote:
> 
> > Hi,
> > 
> > How can we use an array created in Ruby in Java script ?
> > 
> > Regards,  
> > Prashant
> 
> Hi.
> 
> JSON library makes it possible.
> 
> For example:  
> [http://flori.github.com/json/](http://flori.github.com/json/)
> 
> Usage:  
> puts %w!foo bar baz!.to\_json
