Search code examples
ruby-on-railsrubybundlergemfile

Kernel.system call from rails


I am invoking a shell script using Kernel.system from my Rails controller. The shell script might invoke another Ruby script based on some conditions. This Ruby script requires the twitter gem. My Rails app is running in apache using Passenger. Now when this Ruby script is invoked from my Rails app, I get the following error in apache logs.

/var/www/webapps/test/twitter/twitter_post.rb:2:in `require': no such file to load -- twitter (LoadError)
        from /var/www/webapps/test/twitter/twitter_post.rb:2

The same Ruby script runs fine from the Linux shell. Now, if I list the twitter gem in my Gemfile, it works perfectly. Kernel.system is supposed to invoke the commands in a subshell, so is Rails modifying any environment variables in its execution shell?


Solution

  • A subshell has the same environment as the process that spawned it, so the right thing is occurring here, since Bundler overwrites Ruby's load path with its own to ensure only the gems in the Gemfile get loaded.

    If your app depends on this process running, and that process depends on the twitter gem, why is it not in your Gemfile anyway? Further, why are you executing Ruby in a subshell from Ruby in the first place? There's usually no reason you shouldn't just have that Ruby code within your app.