16

I was working on a project and was having issues with therubyracer and libv8 often, so i decided to remove them. It seemed to be the only thing that used them as a dependency was less-rails which I wanted to remove anyways.

My main question is what are they for and do I need them in the average application and if so why?

Weston Ganger
  • 6,324
  • 4
  • 41
  • 39
  • 2
    They are generally used for the asset pipeline on platforms that don't have a JavaScript runtime installed by default. So if you do not compile assets on the target system or the system has a JavaScript runtime in the path they are not needed. – Doon Jan 14 '15 at 00:59
  • So a CentOS server that is not connected to the internet would or would not have this? – Weston Ganger Jan 14 '15 at 01:16
  • If the server already has a JavaScript runtime such as node.js already installed it isn't needed. I don't know if centos comes with a JavaScript runtime also if you are not compiling assets on te box it isn't needed. Less.rb has a dependency on commonjs which appears to only use therubyracer or therubyrhino Check out the answer here for more info. http://stackoverflow.com/questions/18687575/why-is-the-gem-therubyracer-commented-out-by-default-in-gemfile – Doon Jan 14 '15 at 04:29

2 Answers2

14

There are a few things for which a Rails app might use a Javascript Runtime:

1) The Rails Asset Pipeline requires a Javascript Runtime in order to perform Javascript compression.

2) Certain Rails ActionView Helpers like javascript_include_tag require a Javascript Runtime in order to execute Javascript functions.

Not every Rails application use those features, hence the reason that therubyracer gem is initially commented out in your Gemfile; Furthermore note that you could alternatively use NodeJS as your Javascript runtime.

For details, see:

http://guides.rubyonrails.org/asset_pipeline.html#javascript-compression

http://www.rubydoc.info/docs/rails/ActionView/Helpers/JavaScriptHelper

http://www.rubydoc.info/docs/rails/ActionView%2FHelpers%2FAssetTagHelper%3Ajavascript_include_tag

Proper "Rails" way to perform javascript on specific pages

Community
  • 1
  • 1
l3x
  • 30,760
  • 1
  • 55
  • 36
4

rubyracer provides following features:

 1. We can write your ruby codes inside js codes. 
 2. We can invoke the js functions from ruby codes.  
 3. Manipulation of javscript objects & the passing them to javascript functions.

Those are the few basic benefits of using rubyracer gem. Please read complete details here

Ajay
  • 4,199
  • 4
  • 27
  • 47
  • item 1, does that have anything to do with js.erb files? – Weston Ganger Jan 14 '15 at 01:05
  • when you are hitting your rails controller (via ajax) and want some interaction you need to your codes in your app/views/action_name.js.erb file. This file will hold your js codes + embedded ruby codes as well. :) Please check : http://richonrails.com/articles/basic-ajax-in-ruby-on-rails – Ajay Jan 14 '15 at 01:10
  • In the provided link just search for app/views/products/_save.js.erb: That will clear your doubt :) – Ajay Jan 14 '15 at 01:11
  • 2
    sounds terrible, why would we want to invoke ruby code form js or vice versa ? – Nishant Mar 15 '16 at 04:49
  • 1
    OOPS!! I take my words back... I had to run JS inside Java (to port a template system to java https://github.com/nishants/jeyson, https://github.com/nishants/wirestub) ..If I ever need Sinatra/Rails port, will have to try the rubyracer...... too bad there is no pure ruby alternative like Nashorn for Java . – Nishant Oct 20 '16 at 19:20