# Ruby Array to Java Script Array

**URL:** <https://www.ruby-forum.com/t/ruby-array-to-java-script-array/181783>\
**Category:** Rails\
**Created:** [January 18, 2010, 7:33am UTC](https://www.ruby-forum.com/t/ruby-array-to-java-script-array/181783 "2010-01-18T07:33:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [January 18, 2010, 7:33am UTC](https://www.ruby-forum.com/t/ruby-array-to-java-script-array/181783/1 "2010-01-18T07:33:42Z")

</div>

Hi,

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

Regards,  
Prashant

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [January 18, 2010, 9:52am UTC](https://www.ruby-forum.com/t/ruby-array-to-java-script-array/181783/2 "2010-01-18T09:52:25Z")

</div>

2010/1/18 Prashant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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

Convert it to appropriate strings and include it in the script.  
Remember that the view template is run in the server generating text  
that is passed to the browser. Then the javascript is run in the  
browser.

Colin

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [January 18, 2010, 11:12am UTC](https://www.ruby-forum.com/t/ruby-array-to-java-script-array/181783/3 "2010-01-18T11:12:18Z")

</div>

Colin L. wrote:

> 2010/1/18 Prashant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Hi,
> > 
> > How can we use an array created in Ruby in Java script ?
> 
> Convert it to appropriate strings and include it in the script.

You can retrieve the appropriate string by simply saying:

array.to\_json

Consider using something like

\<%= javascript\_tag( yield :js\_declarations ) %\>

in your application.html.erb and

\<% content\_for :js\_declarations, “var array = #{@array.to\_json};” %\>

in the view.

Regards, T.

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [January 18, 2010, 1:04pm UTC](https://www.ruby-forum.com/t/ruby-array-to-java-script-array/181783/4 "2010-01-18T13:04:23Z")

</div>

2010/1/18 Prashant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â Â Â Â Â Â Â Â alert(files[i]);  
> Â Â Â Â Â }
> 
> I am making some mistake in using json but could not figure out what.

Have a look at the html source of the page (View/Page Source or  
similar in browser) and see what you generating. Then you should be  
able to work out what is wrong.

Colin

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [January 18, 2010, 1:20pm UTC](https://www.ruby-forum.com/t/ruby-array-to-java-script-array/181783/5 "2010-01-18T13:20:24Z")

</div>

Colin L. wrote:

> 2010/1/18 Prashant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Â Â Â Â Â Â Â Â alert(files[i]);  
> > Â Â Â Â Â }
> > 
> > I am making some mistake in using json but could not figure out what.
> 
> Have a look at the html source of the page (View/Page Source or  
> similar in browser) and see what you generating. Then you should be  
> able to work out what is wrong.

For this you should of course know that JS doesn’t only not need  
HTML-escaping but doesn’t understand it. So

var files = ["bla","blubb"];

is syntactically wrong. Should be:

var files = [“bla”,“blubb”];

This means: never use h (\<%h %\>) for Javascript.

T.

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [January 18, 2010, 3:29pm UTC](https://www.ruby-forum.com/t/ruby-array-to-java-script-array/181783/6 "2010-01-18T15:29:33Z")

</div>

Use array.to\_json and send it over to the client. For example, you  
could use an AJAX call to do that.

- Aleksey

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [January 18, 2010, 12:44pm UTC](https://www.ruby-forum.com/t/ruby-array-to-java-script-array/181783/7 "2010-01-18T12:44:29Z")

</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  
@files = Dir.glob("\*.txt").to\_json  
In view file  
var files= (’\<%= h @files -%\>’);  
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.  
for(i=0;i\<3;i++)  
{  
alert(files[i]);  
}

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

---

<div class="post-metadata">

**Author:** ![tiwaripk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ab992/32.png) [@tiwaripk](https://www.ruby-forum.com/u/tiwaripk)\
**Post date:** [January 18, 2010, 3:34pm UTC](https://www.ruby-forum.com/t/ruby-array-to-java-script-array/181783/8 "2010-01-18T15:34:58Z")

</div>

T. N. T. wrote:

> Colin L. wrote:
> 
> > 2010/1/18 Prashant [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> > 
> > > Hi,
> > > 
> > > How can we use an array created in Ruby in Java script ?
> > 
> > Convert it to appropriate strings and include it in the script.
> 
> You can retrieve the appropriate string by simply saying:
> 
> array.to\_json
> 
> Consider using something like
> 
> \<%= javascript\_tag( yield :js\_declarations ) %\>
> 
> in your application.html.erb and
> 
> \<% content\_for :js\_declarations, “var array = #{@array.to\_json};” %\>
> 
> in the view.

That’s kind of a bad way of doing it. Instead, put the JSON in a hidden  
div in your HTML, then have your JS read it from there. This will avoid  
mixing JS and HTML.

> Regards, T.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
