# How to get irb to run code as if it was typed in

**URL:** <https://www.ruby-forum.com/t/how-to-get-irb-to-run-code-as-if-it-was-typed-in/115367>\
**Category:** Ruby\
**Created:** [September 24, 2007, 2:34pm UTC](https://www.ruby-forum.com/t/how-to-get-irb-to-run-code-as-if-it-was-typed-in/115367 "2007-09-24T14:34:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [September 24, 2007, 2:34pm UTC](https://www.ruby-forum.com/t/how-to-get-irb-to-run-code-as-if-it-was-typed-in/115367/1 "2007-09-24T14:34:38Z")

</div>

it seems that we can

load “some\_file.rb” or require “some\_file.rb”

in irb to run the program. But after the run, i and a are both  
undefined.  
$g and foo on the other hand, are both defined. how can we make i and a  
both defined, just like we typed it in irb?

i = 10  
a = [1,2,3]  
$g = 100

def foo(i)  
puts i + 10000  
end

p i  
p a  
p $g  
p foo(i)

irb(main):001:0\> load “test\_irb.rb”  
10  
[1, 2, 3]  
100  
10010  
nil  
=\> true

irb(main):002:0\> $g  
=\> 100

irb(main):003:0\> i  
NameError: undefined local variable or method `i’ for main:Object  
from (irb):3

irb(main):004:0\> a  
NameError: undefined local variable or method `a’ for main:Object  
from (irb):4

irb(main):005:0\> foo(123)  
10123  
=\> nil
