1

Note: If you think of a better title/question, feel free to suggest it. I wasn't sure how to articulate this question in one brief sentence.

I created a command line Mastermind game. To play the game, you type play.rb at the command line.

play.rb is a Ruby script that fires up the game. Within the script, the game is sent an interface, called CommandLineInterface.

If you want to play using a GUI (I'm using a Ruby GUI called Limelight), you cd into the limelight directory and type limelight open production and the GUI opens.

There is a mastermind_game directory that contains a lib, a spec, and a limelight directory. The limelight directory contains a production directory.

Now I'm making a few changes. You can pass arguments to the script at the command line. Either you enter play.rb "command line game" or play.rb "limelight game".

ARGV is an array of the arguments passed at the command line.

if ARGV.include?("command line game")
    interface = CommandLineInterface.new
elsif ARGV.include?("limelight game")
    interface = LimelightInterface.new
end

If I want to play my command line game, I enter play.rb "command line game" and it works fine.

I want to be able to type play.rb "limelight game" at the command line and have that open the GUI. In ARGV, the argument "limelight game" would be found so interface would be set to LimelightInterface.new. Within my LimelightInterface class I want the initialize method to open the GUI. It should essentially have the same functionality as typing limelight open production at the command line.

I'm not sure if this is possible or how to do it, so any help would be appreciated! Thanks!

EDITED: I'm trying to execute the command rvm use jruby by including this line in my script:

system("rvm use jruby")

I get back: "RVM is not a function, selecting rubies with 'rvm use ...' will not work."

rzv
  • 2,022
  • 18
  • 22
  • The system command to select a kind of Ruby shouldn't be necessary; you can choose jruby by using a [.rvmrc](http://bre.overnothing.com/workflow/rvmrc/) file. You can store what you're trying to call in a subdirectory and create a `.rvmrc` file there. – Robert K Sep 10 '12 at 19:59
  • Okay, I created a .rvmrc file to specify that I want to use jruby. So all I should have to do is add `system("cd limelight")` to my script, correct? Just `cd` into the directory and the .rvmrc file will run jruby. I'm trying to do that, but it doesn't appear to be successfully going into that directory. – rzv Sep 10 '12 at 20:09
  • No, an `.rvmrc` file determines *which* Ruby is run when calling `ruby ...`, it does not launch Ruby. You need to `cd` into the directory, then start your script. – Robert K Sep 10 '12 at 20:16
  • Okay. I added `Dir.chdir("limelight") do` `system("limelight open production")` `end` . It `cd`'s into the limelight directory, but it doesn't use jruby. When I `cd` into the limelight directory manually, however, it use jruby. – rzv Sep 10 '12 at 20:36
  • Then that's because the script is executed not as your user account but as something else. Try something like `whoami` and compare what your script says against your usual user account. – Robert K Sep 10 '12 at 20:46
  • Hmmm...it's the same for both (normal and within my script). The error I get is `sh: limelight: command not found` – rzv Sep 10 '12 at 20:58
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16513/discussion-between-rzv-and-robert-k) – rzv Sep 10 '12 at 21:06

2 Answers2

0

Here's the first result from googling the title: http://tech.natemurray.com/2007/03/ruby-shell-commands.html

If that's not what you need, I don't understand the question.

rcrogers
  • 2,281
  • 1
  • 17
  • 14
  • Yeah, originally my question wasn't worded this way. I was struggling to find an answer on Google because I wasn't sure *what* to Google. After changing the question several times, I finally landed on "Execute shell commands from Ruby code". I did notice that article just a half hour ago so I'm looking over it now. Now I understand how to use a few things, like backticks and `system`, but I haven't been able to get it to work for my example. – rzv Sep 10 '12 at 19:15
0

Ryan, there's several ways to call out to the system:

Backticks: ruby -e 'p ARGV' '1 2' '3 4' # => "[\"1 2\", \"3 4\"]\n"

The %x literal (note that you can use any delimiter you like, you're not restricted to parentheses)

%x(ruby -e 'p ARGV' '1 2' '3 4') # => "[\"1 2\", \"3 4\"]\n"

The system command. The difference here is that it passes stdin / out / err on through. (the above return the stdout, this one prints it on your process' stdout).

system('ruby', '-e p ARGV', '1 2', '3 4')
# >> ["1 2", "3 4"]

And if you need more sophisticated usage, something like open3 from the stdlib has gotten me pretty far. If you really need the big guns (it doesn't sound like you do), there's a gem open4.


Edit:

It sounds like you're wanting to do something like this:

require 'open3'

bash_script = <<SCRIPT
source "$HOME/.rvm/scripts/rvm"
rvm use jruby
ruby -v
exit
SCRIPT

out, err, status = Open3.capture3 'bash', stdin_data: bash_script
puts out
# >> Using /Users/joshcheek/.rvm/gems/jruby-1.6.7
# >> jruby 1.6.7 (ruby-1.8.7-p357) (2012-02-22 3e82bc8) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_29) [darwin-x86_64-java]

But honestly, I don't think it's a good solution for your situation, because there's many legitimate ways to set up jruby for your environment. I think it would be better to just check that the limelight binary exists, and tell your user to fix their environment if it doesn't.

Joshua Cheek
  • 30,436
  • 16
  • 74
  • 83
  • Thanks, that's helpful. I played around with those to see what I could do and the issue I'm running into is that I can't do `shell("rvm use jruby")`. It responds with: "RVM is not a function, selecting rubies with 'rvm use ...' will not work." So the command I'd like to call is `limelight open`, but to do that I first have to do `rvm use jruby`. Not sure how to make that work. – rzv Sep 10 '12 at 19:21
  • I found [this SO post](http://stackoverflow.com/questions/5792707/cannot-change-rvm-gemset-from-shell-script-via-rvm-gemset-use), but I don't really understand the solution they offered there. – rzv Sep 10 '12 at 19:29