# Execute Ruby Script

**URL:** <https://discuss.rubyonrails.org/t/execute-ruby-script/44246>\
**Category:** rubyonrails-talk\
**Created:** [July 14, 2009, 9:20am UTC](https://discuss.rubyonrails.org/t/execute-ruby-script/44246 "2009-07-14T09:20:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [July 14, 2009, 9:20am UTC](https://discuss.rubyonrails.org/t/execute-ruby-script/44246/1 "2009-07-14T09:20:00Z")

</div>

Yes, it’s possible to execute a Ruby script from Rails. Please remember

that Rails is implemented in Ruby and you can use an method called ‘system’.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![Con](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Con](https://discuss.rubyonrails.org/u/Con)\
**Post date:** [July 14, 2009, 10:16am UTC](https://discuss.rubyonrails.org/t/execute-ruby-script/44246/2 "2009-07-14T10:16:17Z")

</div>

> Hi there,
> 
> now i tried to execute via System-Command but i just can catch the value
> 
> true/false.

Are you doing something like this?

exit\_status = system( \<some\_command\>)

> Is it possible to declare a global variable in my ruby script - execute
> 
> the script - and then catch it with rails?

Yes, it’s possible to declare a global variable within your script. I would

recommend getting yourself a copy of “Programming Ruby” as a desk-side

reference.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![James\_B\_Byrne](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@James\_B\_Byrne](https://discuss.rubyonrails.org/u/James_B_Byrne)\
**Post date:** [July 15, 2009, 9:15pm UTC](https://discuss.rubyonrails.org/t/execute-ruby-script/44246/3 "2009-07-15T21:15:45Z")

</div>

What you are looking for is the backtick '`' method of Kernel, aliased as %x(). You should refer to the official api at [Index of Files, Classes & Methods in Ruby 3.1.2 (Ruby 3.1.2)](http://ruby-doc.org/core/) and read this carefully.

Rails is a framework not a programming language. So it is really a default directory layout supported by a vast amount of clever code to do most of the grunt work required to write web apps. As other have pointed out, Ruby is the language used. To run one Ruby script from another your would have something that looked like this, more or less.

caller.rb #!/usr/bin/env ruby # call the other script and open an new shell %x(path/to/my/other/script.rb) #eof

script.rb #!/usr/bin/env ruby puts "script.rb has been called" #eof

Now, this will display nothing because the stdout of the target script is not looked for in the calling script, which is the one you ahve open in your console. So:

caller.rb #!/usr/bin/env ruby # call the other script and open an new shell my\_stdout = %x(path/to/my/other/script.rb) puts my\_stdout #eof

That should display the output of script.rb on the console running caller.rb. script.rb's stderr can be redirected as well:

caller.rb #!/usr/bin/env ruby # call the other script and open an new shell my\_stdout = %x(path/to/my/other/script.rb 2\> &1) puts my\_stdout #eof

HTH

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 15, 2009, 11:03pm UTC](https://discuss.rubyonrails.org/t/execute-ruby-script/44246/4 "2009-07-15T23:03:41Z")

</div>

James Byrne wrote: [...]

> What you are looking for is the backtick '`' method of Kernel, aliased as %x().

[...]

This shells out just like system() does. It's a good general method of running arbitrary shell scripts in other languages, but it should not be necessary when the scripts are in Ruby. Integrate instead of shelling out.

[...]

> To run one Ruby script from another your would have something that looked like this, more or less.
> 
> caller.rb #!/usr/bin/env ruby # call the other script and open an new shell %x(path/to/my/other/script.rb)

No. You'd be better off using require or similar.

Best,

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 16, 2009, 2:38am UTC](https://discuss.rubyonrails.org/t/execute-ruby-script/44246/5 "2009-07-16T02:38:46Z")

</div>

Marnen Laibow-Koser wrote:

> James Byrne wrote:

> No. You'd be better off using require or similar.

That really depends upon the question being asked. The OP explicitly asked on how to run an external script, that happened to be written in Ruby; not, what is the best way to run arbitrary external ruby code from inside a running Ruby script?

If the latter case then I suggest that the OP consider this approach, which was recently shown to me by Matt Wynne on the Cucumber list:

First, write the script and encapsulate its entire code within an execute method contained in a Script class file. Like so:

script.rb class Script # should be the actual name of the script. &nbsp;&nbsp;def initialize(argv) # receive the ARGV array from the calling process &nbsp;&nbsp;&nbsp;&nbsp;@argv = argv &nbsp;&nbsp;end &nbsp;&nbsp;def execute &nbsp;&nbsp;&nbsp;&nbsp;# if required then parse any command line arguments in @argv here &nbsp;&nbsp;&nbsp;&nbsp;puts "script.rb called" &nbsp;&nbsp;end end #eof

If a stand-alone script initiation is required, as with cron say, then write a stub script that calls the script class and executes the script code like so:

runner.rb #!/usr/bin/env ruby require 'script'

Script.new(ARGV).execute #eof

If the encapsulated script is complex enough that it contains its own internal classes then the whole can be encapsulated inside an outer class given the script name. The inner class containing the execute method is then renamed to something like Main. In this case the call becomes:

Script::Main.new(ARGV).execute

And the class file script.rb has this structure

class Script

&nbsp;&nbsp;class Main &nbsp;&nbsp;&nbsp;&nbsp;def initialize(argv) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@argv = argv &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;def execute &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;script code goes here &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;class OtherClass &nbsp;&nbsp;&nbsp;&nbsp;... &nbsp;&nbsp;end end

One may now use the script within any other ruby process as you suggest, via a simple require and subsequent instantiation. I found this technique extremely valuable in testing since out of process scripts did not share the dbms state with the the test harness.
