# Calling a dynamically generated Ruby script from a .rb

**URL:** <https://www.ruby-forum.com/t/calling-a-dynamically-generated-ruby-script-from-a-rb/100446>\
**Category:** Ruby\
**Created:** [July 28, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/calling-a-dynamically-generated-ruby-script-from-a-rb/100446 "2006-07-28T22:58:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 28, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/calling-a-dynamically-generated-ruby-script-from-a-rb/100446/1 "2006-07-28T22:58:45Z")

</div>

I have a Ruby script that dynamically generates a Ruby script. I would  
like to execute the dynamically generated script from within my script.  
I have some ideas but what’s the best way to do this? Got example code?  
This has to work in a cross-platform environment (Windows, Linux, Mac)

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 28, 2006, 11:07pm UTC](https://www.ruby-forum.com/t/calling-a-dynamically-generated-ruby-script-from-a-rb/100446/2 "2006-07-28T23:07:28Z")

</div>

Shouldn’t it just be as simple as picking a specific name, and then:

require name

?

M.T.

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 28, 2006, 11:07pm UTC](https://www.ruby-forum.com/t/calling-a-dynamically-generated-ruby-script-from-a-rb/100446/3 "2006-07-28T23:07:39Z")

</div>

Or, maybe, in the generated file, have the main bit of code you want  
to execute defined in a function, and call it after requiring it?

M.T.

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 29, 2006, 12:36am UTC](https://www.ruby-forum.com/t/calling-a-dynamically-generated-ruby-script-from-a-rb/100446/4 "2006-07-29T00:36:39Z")

</div>

Also, another option could be to call…

system “ruby #{name}”

(where name is a trusted name (including .rb) and isn’t potentially  
dangerous user-input data).

Another option is to look at \_Why the Lucky Stiff’s Sandbox (freaky  
freaky snadbox, its endearing title) for something a bit more secure  
for when you do need to deal with input from the user.

Hope this helps as well.

M.T.

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 29, 2006, 2:39am UTC](https://www.ruby-forum.com/t/calling-a-dynamically-generated-ruby-script-from-a-rb/100446/5 "2006-07-29T02:39:08Z")

</div>

On windows:

C:\>echo class Fruit \> module.rb  
C:\>echo def orange(taste) \>\> module.rb  
C:\>echo return "oranges tates " + taste.to\_s \>\> module.rb  
C:\>echo end \>\> module.rb  
C:\>echo end \>\> module.rb  
C:\>irb  
irb(main):002:0\> load ‘module.rb’  
=\> true  
irb(main):003:0\> puts Fruit.new.orange(“sweet!”)  
oranges tates sweet!  
=\> nil  
irb(main):004:0\>

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 30, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/calling-a-dynamically-generated-ruby-script-from-a-rb/100446/6 "2006-07-30T17:38:36Z")

</div>

Thanks but it doesn’t seem to work from a script. Just from irb:

% cat rtest.rb

#!/usr/bin/ruby -w

f = File.new(“newfile.rb”, “w+”)  
f.puts(“class MyClass”)  
f.puts(“def run()”)  
f.puts(“print “hello world””)  
f.puts(“end”)  
f.puts(“end”)  
load “newfile.rb”  
MyClass.new.run()

% ./rtest  
./rtest:10: uninitialized constant MyClass (NameError)

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 31, 2006, 8:27am UTC](https://www.ruby-forum.com/t/calling-a-dynamically-generated-ruby-script-from-a-rb/100446/7 "2006-07-31T08:27:50Z")

</div>

Robert La ferla wrote:

> I have a Ruby script that dynamically generates a Ruby script. I would  
> like to execute the dynamically generated script from within my script.  
> I have some ideas but what’s the best way to do this? Got example code?  
> This has to work in a cross-platform environment (Windows, Linux, Mac)
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Why not just eval the generated code?

code = “class A; def to\_s; “B”; end; end”  
eval code  
puts A.new.to\_s

---

<div class="post-metadata">

**Author:** ![rlorimor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@rlorimor](https://www.ruby-forum.com/u/rlorimor)\
**Post date:** [July 30, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/calling-a-dynamically-generated-ruby-script-from-a-rb/100446/8 "2006-07-30T17:45:48Z")

</div>

> > > > > “R” == Robert La ferla [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

R\> f = File.new(“newfile.rb”, “w+”)  
R\> f.puts(“class MyClass”)  
R\> f.puts(“def run()”)  
R\> f.puts(“print "hello world"”)  
R\> f.puts(“end”)  
R\> f.puts(“end”)

f.close

R\> load “newfile.rb”  
R\> MyClass.new.run()

Guy Decoux
