Can I require an external .js file in Ruby

Hello,

Is there a way to include .js functions within my ruby script?

Thanks

Nicole,

In short: no, there isn’t.

What are you trying to accomplish? We might be able to help you with
this
information.


Carlos A.

Control engineering
Polytechnic School, University of So Paulo, Brazil
Computer engineering
Embry-Riddle Aeronautical University, USA

2013/1/8 Nicole V. [email protected]

On 01/08/2013 04:54 PM, Nicole V. wrote:

Hello,

Is there a way to include .js functions within my ruby script?

The Ruby interpreter can only directly run Ruby code. Javascript is not
Ruby, obviously, so the only possible solution would be to find a way to
load a Javascript interpreter and hook it up such that you can call into
it for the functionality you need.

You can probably do that most easily with JRuby since there are some
Javascript interpreters available that run under Java (where JRuby
itself runs), but I couldn’t guess at how hard or easy it would really
be to pull off.

-Jeremy

On Tue, Jan 8, 2013 at 3:19 PM, Jeremy B. [email protected] wrote:

Is there a way to include .js functions within my ruby script?

The Ruby interpreter can only directly run Ruby code. Javascript is not
Ruby, obviously, so the only possible solution would be to find a way to
load a Javascript interpreter

e.g. gem install johnsonhttps://github.com/jbarnette/johnson

Note: haven’t used this in a long time, so can’t vouch for suitability
for whatever particular purpose the OP has in mind. :slight_smile:

Thanks for all of your responses. I guess I would need to access the DOM
somehow. I am doing some automation using selenium-webdriver and ruby.
I am getting stuck on the part to upload an audio because it is a flash
object that I can not interact with. So the developer created some js
functions to get me through those clicks. The file with the functions
that I am calling from my script is supposed to be on the audio upload
page for ex. adCreationModule.js. So, I guess I need to get a
reference to the DOM. I’m just not sure how to do that in Ruby.

That is something completely different. What you’d like to do is
evaluate javascript in webdriver. That is possible.

See:

http://code.google.com/p/selenium/wiki/RubyBindings

On Jan 9, 2013, at 12:54 AM, Hassan S.
[email protected] wrote:

Note: haven’t used this in a long time, so can’t vouch for suitability
for whatever particular purpose the OP has in mind. :slight_smile:

There are multiple mature ways to embed a JS interpreter in Ruby:

gem install therubyracer # V8 embedded in Ruby
gem install therubyrhino # the same for JRuby, using Rhino

See https://github.com/cowboyd/therubyracer for the README

Be aware that this only gives you the javascript interpreter, not access
to DOM manipulations etc.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs