# Executing a ruby file

**URL:** <https://www.ruby-forum.com/t/executing-a-ruby-file/161798>\
**Category:** Rails\
**Created:** [March 2, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/executing-a-ruby-file/161798 "2009-03-02T15:14:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 2, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/executing-a-ruby-file/161798/1 "2009-03-02T15:14:39Z")

</div>

Hi,

I am a newbie to ruby and want to execute a ruby file that is provided  
as input. I want to be able to redirect the output of the execution to a  
log file as well.

I am basically looking at executing a test.rb file which could contain  
(Sample only)

print “hello world”  
sum=2+3  
print sum

I tried kernel eval but will need to do this per line and also I am not  
able to access sum in the print in 3rd line.

I also tried pipe open but then could not redirect the output to a file.

Any help in this regard would be very helpful.

Thanks,  
Sudhindra

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 2, 2009, 3:29pm UTC](https://www.ruby-forum.com/t/executing-a-ruby-file/161798/2 "2009-03-02T15:29:08Z")

</div>

On Mon, Mar 2, 2009 at 6:14 AM, Sudhi K. \<  
removed\_email\_address@domain.invalid\> wrote:

> ## print “hello world” Thanks, Sudhindra

> **[LMGTFY - Let Me Google That For You](https://lmgtfy.app/?q=execute%2Bruby%2Bfile)**
>
> For all those people who find it more convenient to bother you with their question rather than to Google it for themselves.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 2, 2009, 3:51pm UTC](https://www.ruby-forum.com/t/executing-a-ruby-file/161798/3 "2009-03-02T15:51:01Z")

</div>

When you say it’s provided as input, where does this input come from?  
Users? If so, well… I really hope you know what you’re doing.

–Michael

On Mon, Mar 2, 2009 at 8:14 AM, Sudhi K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## print “hello world” Thanks, Sudhindra
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
(Ruby, Rails, Random) blog: [http://skandragon.blogspot.com/](http://skandragon.blogspot.com/)

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 2, 2009, 5:40pm UTC](https://www.ruby-forum.com/t/executing-a-ruby-file/161798/4 "2009-03-02T17:40:14Z")

</div>

Michael G. wrote:

> When you say it’s provided as input, where does this input come from?  
> Users? If so, well… I really hope you know what you’re doing.
> 
> –Michael
> 
> On Mon, Mar 2, 2009 at 8:14 AM, Sudhi K.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## print “hello world” Thanks, Sudhindra
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> > 
> > >
> 
> –  
> (Ruby, Rails, Random) blog: [http://skandragon.blogspot.com/](http://skandragon.blogspot.com/)

Hi,  
Conrad, I was looking for executing from within a ruby file execution  
context and was not able to find good help when I googled for the  
information.  
Michael, Yes I know what kind of scripts would be provided, they would  
be pretty secure

Looks like I can use load ‘test.rb’ to execute the ruby file from  
another ruby program. How do I redirect the output to a file instead of  
stdout and stderr?

Thanks,  
Sudhindra  
Thanks for the replies

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 3, 2009, 4:39am UTC](https://www.ruby-forum.com/t/executing-a-ruby-file/161798/5 "2009-03-03T04:39:03Z")

</div>

Conrad T. wrote:

> On Mon, Mar 2, 2009 at 8:40 AM, Sudhi K. \<  
> removed\_email\_address@domain.invalid\> wrote:
> 
> > > > print “hello world”  
> > > > –  
> > > > another ruby program. How do I redirect the output to a file instead of  
> > > > stdout and stderr?
> > 
> > Thanks,  
> > Sudhindra  
> > Thanks for the replies
> 
> Hi, you should be able to do the following:
> 
> In file “a”, I have the following:
> 
> def a  
> "Hello, "  
> end
> 
> In file “b”, I have the following:
> 
> def b  
> "my name is "  
> end
> 
> In file “c”, I have the following:
> 
> require “a”  
> require “b”
> 
> if **FILE** == $0
> 
> if ARGV.length == 1
> 
> ```
> puts "#{a}#{b}#{ARGV.first}"
> 
> ```
> 
> else
> 
> ```
> puts "Usage: c < your_name >"
> 
> ```
> 
> end
> 
> end
> 
> end
> 
> > > ruby c Sudhindra \>& file.txt
> 
> file.txt contains: Hello, my name is Sudhindra
> 
> Lastly, I would recommend reading about global constants in “Programming  
> Ruby 1.8” or “Programming Ruby 1.9”. Also, I would get a good reference  
> on  
> the Unix  
> command set.
> 
> Good luck,
> 
> -Conrad

Hi,

May be I am not putting the question right. I want to have editor where  
I type in text(Ruby) and that is copied to a temp file for execution.  
Now within my editor which is also a ruby script I want to execute this  
file. So I call

load ‘temp.rb’

The problem is that this program executes fine but I am not able to  
capture the output into a file.

Can I do something like

load 'temp.rb \>&output.txt ? ( This did not work so what should I do?)

Thanks,  
Sudhindra

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 2, 2009, 6:47pm UTC](https://www.ruby-forum.com/t/executing-a-ruby-file/161798/6 "2009-03-02T18:47:41Z")

</div>

On Mon, Mar 2, 2009 at 8:40 AM, Sudhi K. \<  
removed\_email\_address@domain.invalid\> wrote:

> > > print “hello world”  
> > > –  
> > > another ruby program. How do I redirect the output to a file instead of  
> > > stdout and stderr?
> 
> Thanks,  
> Sudhindra  
> Thanks for the replies

Hi, you should be able to do the following:

In file “a”, I have the following:

def a  
"Hello, "  
end

In file “b”, I have the following:

def b  
"my name is "  
end

In file “c”, I have the following:

require “a”  
require “b”

if **FILE** == $0

if ARGV.length == 1

```
puts "#{a}#{b}#{ARGV.first}"

```

else

```
puts "Usage: c < your_name >"

```

end

end

end

> > ruby c Sudhindra \>& file.txt

file.txt contains: Hello, my name is Sudhindra

Lastly, I would recommend reading about global constants in “Programming  
Ruby 1.8” or “Programming Ruby 1.9”. Also, I would get a good reference  
on  
the Unix  
command set.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 3, 2009, 7:53am UTC](https://www.ruby-forum.com/t/executing-a-ruby-file/161798/7 "2009-03-03T07:53:24Z")

</div>

Hi Sudhindra,

Depending on the needs/requirements of what you’re trying to do, you  
could probably get away with exec’ing your ruby file in a subshell and  
direct output as needed.

Take a look at Kernel module backtics ([RDoc Documentation](http://www.ruby-doc.org/core/)  
classes/Kernel.html#M006001) and read up on various options regarding  
(sub)processes (altho old pickaxe book …  
[http://www.rubycentral.com/pickaxe/tut\_threads.html](http://www.rubycentral.com/pickaxe/tut_threads.html)).

Simple example:

$ cat sum.rb  
puts “#{ARGV[0].to\_i} + #{ARGV[1].to\_i} = #{ARGV[0].to\_i + ARGV  
[1].to\_i}”

$ ruby sum.rb 4 5  
4 + 5 = 9

$ irb

> `ruby sum.rb 3 4 > sum_output.txt 2>&1`  
> =\> “”

> res = IO.read(‘sum\_output.txt’)  
> =\> “3 + 4 = 7\n”

Note however that using backtics you can directly capture the output  
of what was exec’d in the subshell if you want, instead of writing it  
to some file and then reading it back in:

> res = `ruby sum.rb 3 4 2>&1`.chomp  
> =\> “3 + 4 = 7”

That said, I second Michael’s hope-you-know-what-you’re-doing.  
Cheers,

Jeff

On Mar 2, 7:39 pm, Sudhi K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 3, 2009, 5:59pm UTC](https://www.ruby-forum.com/t/executing-a-ruby-file/161798/8 "2009-03-03T17:59:46Z")

</div>

Just be VERY VERY careful when using back-ticks or any other form of  
Kernel.system().

For instance, since this is a Rails list, I assume you are using,  
well, Rails. Suppose you want to pass the input from a form to a  
subprocess. If you wrote:

book = params[:book]  
cmd = “/usr/local/bin/doit #{book}”  
Kernel.system(cmd)

you will have created a machine that will be “pwned” nearly instantly.

A safer, but still not 100% safe, method is to use an array:

cmd = [“/usr/local/bin/doit”, book]  
Kernel.system(\*cmd)

Here are some examples of the differences:

=\> "/bin/ls `foobar`"

> > Kernel.system(cmd)  
> > sh: foobar: command not found \<-------- NOTE! Security hole!

Compare to:

> > cmd = ["/bin/ls", “`foobar`”]  
> > =\> ["/bin/ls", “`foobar`”]
> 
> > Kernel.system(\*cmd)  
> > ls: `foobar`: No such file or directory \<---- Note (good result)

---

<div class="post-metadata">

**Author:** ![sudhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@sudhi](https://www.ruby-forum.com/u/sudhi)\
**Post date:** [March 4, 2009, 4:26am UTC](https://www.ruby-forum.com/t/executing-a-ruby-file/161798/9 "2009-03-04T04:26:44Z")

</div>

Michael G. wrote:

> Just be VERY VERY careful when using back-ticks or any other form of  
> Kernel.system().
> 
> For instance, since this is a Rails list, I assume you are using,  
> well, Rails. Suppose you want to pass the input from a form to a  
> subprocess. If you wrote:
> 
> book = params[:book]  
> cmd = “/usr/local/bin/doit #{book}”  
> Kernel.system(cmd)
> 
> you will have created a machine that will be “pwned” nearly instantly.
> 
> A safer, but still not 100% safe, method is to use an array:
> 
> cmd = [“/usr/local/bin/doit”, book]  
> Kernel.system(\*cmd)
> 
> Here are some examples of the differences:
> 
> =\> "/bin/ls `foobar`"
> 
> > > Kernel.system(cmd)  
> > > sh: foobar: command not found \<-------- NOTE! Security hole!
> 
> Compare to:
> 
> > > cmd = ["/bin/ls", “`foobar`”]  
> > > =\> ["/bin/ls", “`foobar`”]
> > 
> > > Kernel.system(\*cmd)  
> > > ls: `foobar`: No such file or directory \<---- Note (good result)

Hi,

Michael, Thanks for the inputs. You are right I am using it within  
rails. I dont intend to execute any system commands like ls etc. I would  
want to execute ruby script only, like calling some methods, some print  
commands and some control structures.

Does kernel.system calls not need a new thread and is it handled in  
rails? Also how about using load or popen3? Dont these have any way of  
redirecting the output to a file? I am asking a lot of questions but I  
am still not clear which is the best method to use for executing the  
ruby scripts.
