# Execute ruby commands in a program

**URL:** <https://www.ruby-forum.com/t/execute-ruby-commands-in-a-program/112420>\
**Category:** Ruby\
**Created:** [August 18, 2007, 6:16pm UTC](https://www.ruby-forum.com/t/execute-ruby-commands-in-a-program/112420 "2007-08-18T18:16:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 18, 2007, 6:16pm UTC](https://www.ruby-forum.com/t/execute-ruby-commands-in-a-program/112420/1 "2007-08-18T18:16:28Z")

</div>

Ok so lets say i have a program, And within the program i call a command  
such as Ruby:

Is there anyway to be able to run live ruby code and print the answers  
(i dont really know how to put it) so like…

Ruby: name = ‘john’; puts “Hi” if name == ‘john’  
Program: Hi

Sounds weird i know, But if Anyone out there understands me that would  
be great…

I want to stay away from shell one liners like ruby -e “code here”

Thanks in advance

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 18, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/execute-ruby-commands-in-a-program/112420/2 "2007-08-18T19:26:42Z")

</div>

Um, this sounds like ‘irb’. Depending on what OS you’re on and how  
you installed ruby, you should just be able to type ‘irb’ at the  
command-line. That sounds like what you’re after.

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 18, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/execute-ruby-commands-in-a-program/112420/3 "2007-08-18T20:00:47Z")

</div>

Haze N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Is there anyway to be able to run live ruby code and print the answers  
> (i dont really know how to put it) so like…
> 
> Ruby: name = ‘john’; puts “Hi” if name == ‘john’  
> Program: Hi
> 
> Sounds weird i know, But if Anyone out there understands me that would  
> be great…
> 
> I want to stay away from shell one liners like ruby -e “code here”

I have a bit of trouble understanding what it is you want to have  
happen, especially since you specifically wish to avoid 1-liners.

Is “irb” (bundled with ruby) what you want?

Absent that, at the shell you can just run “ruby”, type some ruby  
code, then type  
**END**  
on a line by itself, (that’s two underscores, then END, then two more  
underscores) and the code will then run.

Is either of this what you wanted?

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 18, 2007, 8:34pm UTC](https://www.ruby-forum.com/t/execute-ruby-commands-in-a-program/112420/4 "2007-08-18T20:34:26Z")

</div>

Sorry its quite hard to explain, Its not IRB, because i want to actually  
run a command from within another ruby script… Lets say i wanted to do  
a 1 liner from within the program i could do something like…

me: ruby puts “Hello”  
script runs `ruby -e “puts ‘Hello’”  
Program: Hello

But i didn’t really want to do any 1 liners, althouth if i did, lets say  
i have

while msg = gets  
if msg =~ /^ruby (.+)/i  
msg.gsub!(/"/, “’”)  
message = `ruby -e "#{$1}"`  
puts message  
end  
end

and input was…

puts ‘hello’

How could i return the input?

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 18, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/execute-ruby-commands-in-a-program/112420/5 "2007-08-18T20:42:50Z")

</div>

On Aug 18, 2007, at 1:34 PM, Haze N. wrote:

> But i didn’t really want to do any 1 liners, althouth if i did,
> 
> and input was…
> 
> puts ‘hello’
> 
> ## How could i return the input?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

look up eval

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 19, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/execute-ruby-commands-in-a-program/112420/6 "2007-08-19T14:06:16Z")

</div>

- Haze N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (21:05) schrieb:

> Thanks, i looked it up and coded something pretty much the same except  
> something like message = eval(“#{$1}”)… when i print out the return it  
> just print nil

What did you expect it to print?

mfg, simon … l

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 18, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/execute-ruby-commands-in-a-program/112420/7 "2007-08-18T20:59:52Z")

</div>

John J. wrote:

> On Aug 18, 2007, at 1:34 PM, Haze N. wrote:
> 
> > But i didn’t really want to do any 1 liners, althouth if i did,
> > 
> > and input was…
> > 
> > puts ‘hello’
> > 
> > ## How could i return the input?
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> look up eval

Thanks, i looked it up and coded something pretty much the same except  
something like message = eval(“#{$1}”)… when i print out the return it  
just print nil
