running ruby system() from rails server including gem access - ruby

I realize the peril I am asking for, but I am setting up a ruby on rails server on a mac using Big Sur to give students access to remote robots they will be able to see with a webcam.
This first one is allowing students to use a ruby library to control the robot. They enter the source code to run through the browser, which is stored in sketch.source_code.
After saving the record in the normal way and writing the source to the filesystem, this code runs on the rails server to try to execute the robot control code:
source_pathname = "/tmp/code.rb"
output_pathname = "/tmp/code.log"
File.write(source_pathname, sketch.source_code) # write the code to execute to the filesystem
env = {}
env['GEM_PATH'] = '/Users/fmorton/.gems:/Users/fmorton/.gem/ruby/2.7.0:/Users/fmorton/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0'
ruby = "ruby #{source_pathname} &>#{output_pathname}"
result = system(env, ruby)
This works fine if the original source code includes the full path to the gem, as in:
require '/Users/fmorton/.gems/gems/birdbrain-0.9.4/lib/birdbrain.rb'
But, if specifying the gem as in:
require 'birdbrain'
the gem is not found.

Related

How can I run a terminal command from a program?

I have a small ruby program that I'm working on at the moment (Pure ruby, not rails), which crawls a small portion of the web for certain movies. Is there a recommended way to, for example, when I run the script from my windows terminal, that it will open up a browser with the url that I extracted?
I know start http://example.com works from the windows terminal but im just wondering if there's a way to invoke a method like this directly from a ruby script.
I've found a nice gem called launchy to do this (at least opening a url)
http://www.rubydoc.info/gems/launchy/2.4.3
In the terminal :
gem install launchy
then:
require 'launchy'
Launchy.open("url")

Using JRuby+Sikuli on Windows

This video shows JRuby+Sikuli working on Windows 7 or higher. There we have to require "java"; require "sikuli".
So I've installed SikuliX using sikuli-setup.jar and did some practice in Sikuli IDE.
However the \jruby-1.7.15\bin\gem install java gives java requires Ruby version >= 2.1.0., since the latest JRuby installer is jruby 1.7.15 (1.9.3p392), so I had to ... install-fjava.
Then I had to manually set ENV variable SIKULI_HOME, but now I have this error:
irb> require "sikuli"
LoadError: Failed to load 'C:\Users\admin\Downloads\nakilon\sikuli/sikuli-script.jar'
Is Sikuli installed?
And yes, I have no sikuli-script.jar in my SikuliX folder.
So what is the proper way to make JRuby+Sikuli[X] to work on Windows 7/8?
UPD:
I just realized, that gem install sikuli installs a 2 years old stuff, that isn't a Sikuli X.
So I did a hack -- renamed sikuli-ide.jar to sikuli-script.jar.
Trying to execute some lines from that video: screen = Sikuli::Screen.new goes fine.
But since script = org.sikuli.script.SikuliScript.new gives:
cannot load Java class org.sikuli.script.SikuliScript
I checked github sources -- there is really no SikuliScript class, so now I'm lost (
Current official JRuby gem way
This question posted as GitHub issue, with solution
Google Drive shared folder with stack of files that seems to work for me
Checksums of files in it:
MD5 (jruby_windows_1_7_15.exe) = 61600abdb47177dc50c4ec887dc6c3f1
MD5 (sikulixsetup-1.1.0-Beta4.jar) = 7b836f4dad2e6817fd599f91cb1b9e7f
MD5 (sikulix-1.1.0.3.gem) = e1083cf09b55c917b62a34cc45cdded0
Google Drive doesn't allow to share .exe, so you'll see only two files.

how to use RVM to a ruby app engine?

i wanna make a simple ruby App Engine in rails just like heroku, i'm dealing with a problem now.
My idea was:
1.use rails to establish the App Engine, use a class 'App' to handle all apps.
2.when a user create an ruby app he should offer it's git path
3.when the user deploys it, my app engine will do these things:
clone the git to a path in my server (done
use RVM to designatine the ruby version witch user wanted and make a gemdir for the project (some problems here
create a nginx conf for the project, then include it and reload nginx (i can do it
Problems in the second step:
codes here:
def start_thin
Dir.chdir(proj_path) do
system('rvm use ruby-1.8.7-p352#testname --create')
system('gem env gemdir')
success = system ('thin start -s3 --socket ' + self.proj_sock)
if success
return true
end
end
return false
end
when the code runs here, the log told me "RVM is not a function...blahblah", i know something about the login-shell and non-login-shell, then i try to fix it via editing .bashrc but same problem occurred.
And if i ignore it, the app can't be deployed, because of a Load Error :
myapp.rb:2:in `require': cannot load such file -- sinatra (LoadError)
if i open a terminal in that app directory, i can use thin to start it.
i wanna know how to run cmd just like in a terminal, without all these odd problem?
or how to edit my method to fix it?
Thanks!
Thanks Casper and GhostRider.
The user and rvm settings are correct.
After lots of googles and tests i found it's impossible...
Finally I fixed it by using RVM's ruby api instead of running system command.
Such as :
require 'rvm'
env = RVM.current
env.gemset.create('app1')

uninitialized constant Twitter (NameError)

Hey guys i have a problem i am facing with the twitter gem. I have a file (twitter.rb) with this content
require "rubygems"
require "twitter"
puts Twitter.user_timeline("roykasa").first.text
puts Twitter.user("roykasa").location
search = Twitter::Search.new
search.containing("hate").to("StewieJokess").
result_type("recent").each do |r| puts r.text end
When i run the file i get this error :
uninitialized constant Twitter (NameError)
I read somewhere on SO where a user had a similar problem and he solved it by installing a new version of ruby and rubygems but the problem i am having is am running suse 12.1 and am running the latest versions of both ruby and ruby gems. No rpms can be found from 3rd parties anywhere. Atleast i have searched. Does anyone know another way round this?
If you are running Ruby 1.8.x you should be able to solve your problem by renaming your own script to anything different than twitter.rb.
This is because the main file in the twitter gem is named exactly like this and your file probably overrides it in combined virtual file system that the $LOAD_PATH order creates. Before Ruby 1.9.x, require did not only load from library directories, but preferred to load files relative to the current working directory of your process, in this case, the directory where your script lies in.
Do not name your file as twitter.rb, also make sure there is no other file in the same directory with the name twitter.rb

Requiring scripts is broken when using RVM

Been running into a problem ever since I started using rvm to manage my ruby installation. Whenever I want to require another class I've written, such as require 'filename', I get a require - no such file to load error when I try to run my script. If I switch back to my system ruby using rvm use system it works again, but I'm 1.8.2 as my system ruby and some features I want to use in my code are only available in 1.9.*, which I can access through rvm. How do I fix this problem?
I'm not sure this is a problem with RVM, but a problem with Ruby 1.9 not including '.' in the current path. For instance, if you are trying to require a library at './lib/file.rb', you can't do
require "lib/file"
You have to do this:
require "./lib/file"
Have you tried that syntax for your require?
I think you want to use require_relative
http://extensions.rubyforge.org/rdoc/classes/Kernel.html

Resources