# Load\_to(file, mod)

**URL:** <https://www.ruby-forum.com/t/load-to-file-mod/60262>\
**Category:** Development\
**Created:** [May 11, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/load-to-file-mod/60262 "2006-05-11T18:04:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://www.ruby-forum.com/u/Hidetoshi_NAGAI)\
**Post date:** [May 11, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/load-to-file-mod/60262/1 "2006-05-11T18:04:07Z")

</div>

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

e$B;XDj$7$?%b%8%e!\<%k2\<$Ke(B load e$B$9$k$?$a$Ke(B subject  
e$B$K\<($7$?$h$&$Je(B  
e$B4X?t7?$N%a%=%C%I$,e(B (e$BL\>A0$NNI$70-$7$OJ]N1$H$7$Fe(B)  
e$BM\_$7$$$J$H;W$C$F$$$^$9!%e(B

# load(file, true) e$B$Oe(B load\_to(file, Module.new) e$B$HF1$8$G$9!%e(B

e$B$=$&$$$&$b$N$,$J$$$h$&$K8+$($k$N$O!$\<B$Oe(B

(1) e$BE,@Z$JJ}K!$,B\>$KB8:_$9$ke(B  
(2) Thread e$B$H$NMm$_$GMn$A$ke(B or  
e$B@5\>o$KF0$+$J$$4m81@-$,$"$k$?$ae(B  
(3) e$B2?$i$+$N%;%-%e%j%F%#%[!\<%k$H$J$j$&$k$?$ae(B  
(4) e$B$=$b$=$b$=$s$J$b$N$,M\_$7$$$H;W$&H/A[\<+BN$,$\*$+$7$$e(B  
(5) e$B$=$NB\>!$FCJL$JM}M3$,B8:\_$9$ke(B

e$B$N$$$:$l$+$^$?$OJ#?t$,@.N)$7$F$$$k$N$Ke(B  
e$BC1$K;d$,5$$,IU$$$F$$$J$$$@$1$J$N$G$7$g$&$+!)e(B

# e$B$A$g$C$HD4$Y$?8B$j$O8+$D$1$k$3$H$,$G$-$J$+$C$?$N$G$9$,!$e(B

# e$B$=$l$bC1$KD4$YJ}$,B-$j$J$$$@$1$J$N$G$7$g$&$+!)e(B(^\_^;

## e$BG0$N$?$a!$%Q%C%A$rE:$($F$\*$-$^$9!%e(B e$B0z?t$,\>/$75$;}$A0-$$$G$9$,!$=\>Mh$H$N8\_49@-0];}$He(B e$BF1$8%3!\<%I$N7+$jJV$72sHr$H$N$?$ae(B ( + e$B\<jH4$-$N$?$ae(B ) e$B$Ke(B e$B$=$&$7$F$^$9!%e(B

```
                                   e$B1J0fe(B e$B=(Mxe(B 

```

e$B!J6e9)Bge(B e$BCNG=\>pJs!Ke(B  
removed\_email\_address@domain.invalid

# Index: eval.c

RCS file: /var/cvs/src/ruby/eval.c,v  
retrieving revision 1.890  
diff -u -r1.890 eval.c  
— eval.c 23 Feb 2006 04:24:39 -0000 1.890  
+++ eval.c 11 May 2006 15:56:35 -0000  
@@ -6618,7 +6618,7 @@  
NORETURN(static void load\_failed(VALUE));

void  
-rb\_load(VALUE fname, int wrap)  
+rb\_load\_to(VALUE fname, int wrap, VALUE wrapper\_mod)  
{  
VALUE tmp;  
int state;  
@@ -6647,8 +6647,14 @@  
ruby\_wrapper = 0;  
}  
else {

- /\* load in anonymous module as toplevel \*/
- ruby\_wrapper = rb\_module\_new();

- 

```
   if (rb_obj_is_kind_of(wrapper_mod, rb_cModule)) {

```

- 

```
       /* load in the module as toplevel */

```

- 

```
       ruby_wrapper = wrapper_mod;

```

- 

```
   }

```

- 

```
   else {

```

- 

```
       /* load in anonymous module as toplevel */

```

- 

```
       ruby_wrapper = rb_module_new();

```

- 

```
   }

```

self = rb\_obj\_clone(ruby\_top\_self);  
rb\_extend\_object(self, ruby\_wrapper);  
PUSH\_CREF(ruby\_wrapper);  
@@ -6712,6 +6718,12 @@  
}

void  
+rb\_load(VALUE fname, int wrap)  
+{

- rb\_load\_to(fname, wrap, Qnil);  
+}
- 

+void  
rb\_load\_protect(VALUE fname, int wrap, int \*state)  
{  
int status;  
@@ -6727,6 +6739,22 @@  
if (state) \*state = status;  
}

+void  
+rb\_load\_protect\_to(VALUE fname, int wrap, VALUE wrapper\_mod, int  
\*state)  
+{

- int status;
- 
- PUSH\_THREAD\_TAG();
- if ((status = EXEC\_TAG()) == 0) {
- rb\_load\_to(fname, wrap, wrapper\_mod);
- }
- else if (status == TAG\_THREAD) {
- rb\_thread\_start\_1();
- }
- POP\_THREAD\_TAG();
- if (state) \*state = status;  
+}
- 

/\*

- call-seq:
- 

```
load(filename, wrap=false) => true

```

@@ -6752,6 +6780,26 @@  
return Qtrue;  
}

- 

+/\*

- 
  - call-seq:

- 
  - 

```
load_to(filename, wrapper) => true

```

- 
  - 

- 
  - Loads and executes the Ruby

- 
  - program in the file _filename_ under the specified module _wrapper_

- 
  - (See also the _wrap_ parameter of `load` function).

- 
  - If the filename does not

- 
  - resolve to an absolute path, the file is searched for in the  
library

- 
  - directories listed in `$:`.

- \*/
- 

+static VALUE  
+rb\_f\_load\_to(VALUE obj, VALUE fname, VALUE wrapper\_mod)  
+{

- rb\_load\_to(fname, 1, wrapper\_mod);
- return Qtrue;  
+}
- 

VALUE ruby\_dln\_librefs;  
static VALUE rb\_features;  
static st\_table \*loading\_tbl;  
@@ -7875,6 +7923,7 @@  
rb\_features = rb\_ary\_new();

```
 rb_define_global_function("load", rb_f_load, -1);

```

- rb\_define\_global\_function(“load\_to”, rb\_f\_load\_to, 2);  
rb\_define\_global\_function(“require”, rb\_f\_require, 1);  
rb\_define\_method(rb\_cModule, “autoload”, rb\_mod\_autoload, 2);  
rb\_define\_method(rb\_cModule, “autoload?”, rb\_mod\_autoload\_p, 1);

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://www.ruby-forum.com/u/Hidetoshi_NAGAI)\
**Post date:** [May 29, 2006, 8:12am UTC](https://www.ruby-forum.com/t/load-to-file-mod/60262/2 "2006-05-29T08:12:01Z")

</div>

e$B@DLZ$G$9!#D6CY%l%9$D$1$^$9e(B

In mail “[ruby-dev:28633] load\_to(file, mod)”  
Hidetoshi NAGAI [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B
> 
> e$B;XDj$7$?%b%8%e!\<%k2\<$Ke(B load e$B$9$k$?$a$Ke(B subject e$B$K\<($7$?$h$&$Je(B  
> e$B4X?t7?$N%a%=%C%I$,e(B (e$BL\>A0$NNI$70-$7$OJ]N1$H$7$Fe(B) e$BM\_$7$$$J$H;W$C$F$$$^$9!%e(B
> 
> # load(file, true) e$B$Oe(B load\_to(file, Module.new) e$B$HF1$8$G$9!%e(B

mod.module\_eval(File.read(path)) e$B$G!“$@$$$?$$F1$8$3$H$,$G$-$^$9!#e(B  
e$B%m!\<%I%Q%9$+$iC5$7$?$$$H$J$k$H!”$b$&\>/$7$d$i$J$$$H$@$a$G$9$1$I$M!#e(B

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://www.ruby-forum.com/u/Hidetoshi_NAGAI)\
**Post date:** [May 29, 2006, 9:41am UTC](https://www.ruby-forum.com/t/load-to-file-mod/60262/3 "2006-05-29T09:41:04Z")

</div>

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Minero A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: [ruby-dev:28669] Re: load\_to(file, mod)  
Date: Mon, 29 May 2006 15:11:01 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > e$B;XDj$7$?%b%8%e!\<%k2\<$Ke(B load e$B$9$k$?$a$Ke(B subject e$B$K\<($7$?$h$&$Je(B  
> > e$B4X?t7?$N%a%=%C%I$,e(B (e$BL\>A0$NNI$70-$7$OJ]N1$H$7$Fe(B) e$BM\_$7$$$J$H;W$C$F$$$^$9!%e(B
> > 
> > # load(file, true) e$B$Oe(B load\_to(file, Module.new) e$B$HF1$8$G$9!%e(B
> 
> mod.module\_eval(File.read(path)) e$B$G!“$@$$$?$$F1$8$3$H$,$G$-$^$9!#e(B  
> e$B%m!\<%I%Q%9$+$iC5$7$?$$$H$J$k$H!”$b$&\>/$7$d$i$J$$$H$@$a$G$9$1$I$M!#e(B

e$B%m!\<%I%Q%9$N7o$O$H$j$"$($:CV$$$F$_$/$H$7$F!$e(B  
e$B$3$l$re(B $SAFE == 4 e$B$G$Ne(B load(path, true)  
e$B$NBe$j$H$7$F;H$_$&$H$9$k$H!$e(B  
File.read e$B$,0z$C3]$+$C$A$c$$$^$9$h$M!)e(B  
e$B$^$?!$e(Bself e$B$,e(B main e$B$G$O$J$/e(B mod  
e$B$K$J$C$F$7$^$$$^$9$h$M!)e(B  
self e$B$,e(B mod e$B$K$J$C$F$7$^$&$He(B path e$B$N%9%/%j%W%H$+$ie(B  
mod e$B$K?($l$F$7$^$&$N$Ge(B (e$B$G$9$h$M!)e(B) e$B5$$KF~$j$^$;$s!%e(B

e$B$d$j$?$$$N$O!$e(B$SAFE == 4 e$B$Ge(B load(path, true)  
e$B$7$?%9%/%j%W%HCf$Ne(B  
load(path2) e$B$rF1$8L5L\>%b%8%e!\<%kCf$GI\>2A$7$?$$$H$$$&$b$N$G$9!%e(B  
e$B$5$i$K$O!$e(Bload() e$B$N30$+$ie(B load  
e$BFb$N4F;k$b$7$?$$$H;W$C$F$^$9!%e(B

load e$B%a%=%C%I$r$/$k$s$G$d$k$3$H$G$"$kDxEY$^$G$O9T$1$k$N$G$9$,!$e(B  
e$BL5L\>%b%8%e!\<%k$,\<hF@$G$-$J$/$F$@$a$@$C$?$j!$e(B  
e$B$=$l$rHr$1$k$He(B self e$B$,e(B main  
e$B$G$O$J$/$J$C$F$7$^$C$?$j$GF,$rG:$^$;$F$^$9!%e(B

mod.module\_eval(File.read(path)) e$B$Ne(B File.read(path) e$B$re(B  
e$B$&$^$/$/$k$s$G$d$C$F!$e(Bself e$B$,e(B mod e$B$K$J$C$F$7$^$&E@$Oe(B  
e$BBE6($9$k$7$+J}K!$O$J$$$N$G$7$g$&$+!%e(B

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://www.ruby-forum.com/u/Hidetoshi_NAGAI)\
**Post date:** [May 29, 2006, 10:29am UTC](https://www.ruby-forum.com/t/load-to-file-mod/60262/4 "2006-05-29T10:29:51Z")

</div>

e$BA0ED$G$9!#e(B

Hidetoshi NAGAI wrote:

> e$BBE6($9$k$7$+J}K!$O$J$$$N$G$7$g$&$+!%e(B  
> generate\_binding.rbe$B$H$$$&L\>A0$Ge(B

----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B -----  
e$B$3$3$+$ie(B -----  
$binding = binding  
----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B -----  
e$B$3$3$^$Ge(B -----

e$B$H$$$&FbMF$N%U%!%$%k$rMQ0U$7$F$\*$$$F!"e(B

----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B -----  
e$B$3$3$+$ie(B -----  
def generate\_binding  
Thread.critical = true  
begin  
load(“generate\_binding.rb”, true)  
return $binding  
ensure  
Thread.critical = false  
end  
end

b = generate\_binding

# 1e$B2sL$N\<B9Te(B

eval(“def hello; puts ‘hello world’; end”, b)

# 2e$B2sL$N\<B9Te(B

eval(“hello”, b)  
----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B -----  
e$B$3$3$^$Ge(B -----

e$B$H$$$&\<j$r;H$&$N$O$I$&$G$7$g$&$+!#e(B

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://www.ruby-forum.com/u/Hidetoshi_NAGAI)\
**Post date:** [May 29, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/load-to-file-mod/60262/5 "2006-05-29T15:46:02Z")

</div>

e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

From: Shugo M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: [ruby-dev:28671] Re: load\_to(file, mod)  
Date: Mon, 29 May 2006 17:27:04 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> generate\_binding.rbe$B$H$$$&L\>A0$Ge(B
> 
> ----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B ----- e$B$3$3$+$ie(B -----  
> $binding = binding  
> ----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B ----- e$B$3$3$^$Ge(B -----
> 
> e$B$H$$$&FbMF$N%U%!%$%k$rMQ0U$7$F$\*$$$F!"e(B  
> (snip)  
> e$B$H$$$&\<j$r;H$&$N$O$I$&$G$7$g$&$+!#e(B

e$B$J$k$[$I!%e(Bbinding e$B$r;H$C$F;}$A=P$7$F$7$^$&\<j$,$“$j$^$7$?$+!%e(B  
e$B3N$+$K$$$1$=$&$J5$$,$7$^$9!%$A$g$C$H9M$($F$\_$^$9!%e(B  
e$B$”$j$,$H$&$4$6$$$^$7$?!%e(B

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://www.ruby-forum.com/u/Hidetoshi_NAGAI)\
**Post date:** [February 3, 2007, 2:10am UTC](https://www.ruby-forum.com/t/load-to-file-mod/60262/6 "2007-02-03T02:10:46Z")

</div>

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:28672] Re: load\_to(file, mod)”  
on Mon, 29 May 2006 18:18:21 +0900, Minero A.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|\> e$B$3$l$re(B $SAFE == 4 e$B$G$Ne(B load(path, true) e$B$NBe$j$H$7$F;H$\*$&$H$9$k$H!$e(B  
|\> File.read e$B$,0z$C3]$+$C$A$c$$$^$9$h$M!)e(B  
|  
|$SAFE=4 e$B$Ge(B load e$B$C$F$G$-$k$s$G$7$?$C$1!#e(B

|1.8 e$B$@$H$G$-$k$s$G$9$M$(!#$I$C$A$,@5$7$$$s$@$m$&!#e(B

e$B$J$s$G%:%l$F$k$s$@$m$&!)e(B e$B;W$$=P$;$J$$!#BhFs0z?t$,;XDj$5$l$Fe(B  
e$B$$$k;~$K$Oe(B$SAFE=4e$B$G$b0BA4$G$O$J$$$+$H;W$C$?5-21$O$"$k$s$G$9e(B  
e$B$1$I!#e(B

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://www.ruby-forum.com/u/Hidetoshi_NAGAI)\
**Post date:** [February 3, 2007, 2:18am UTC](https://www.ruby-forum.com/t/load-to-file-mod/60262/7 "2007-02-03T02:18:46Z")

</div>

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

e$B$D$$!“$&$C$+$j2a5n$N%a!\<%ke(B(2006-05-29)e$B$K%j%W%i%$$7$F$7$^$$$^e(B  
e$B$7$?$,!”$3$N7o!"$$$^$@$KJ|CV$5$l$F$k$s$G$9$h$M$(!#\<B:]$N$H$3e(B  
e$B$m$I$&$J$s$G$7$g!)e(B

In message “Re: [ruby-dev:30247] Re: load\_to(file, mod)”  
on Sat, 3 Feb 2007 10:10:21 +0900, Yukihiro M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:  
|  
|e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B  
|  
|In message “Re: [ruby-dev:28672] Re: load\_to(file, mod)”  
| on Mon, 29 May 2006 18:18:21 +0900, Minero A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:  
|  
||\> e$B$3$l$re(B $SAFE == 4 e$B$G$Ne(B load(path, true) e$B$NBe$j$H$7$F;H$\*$&$H$9$k$H!$e(B  
||\> File.read e$B$,0z$C3]$+$C$A$c$$$^$9$h$M!)e(B  
||  
||$SAFE=4 e$B$Ge(B load e$B$C$F$G$-$k$s$G$7$?$C$1!#e(B  
|  
||1.8 e$B$@$H$G$-$k$s$G$9$M$(!#$I$C$A$,@5$7$$$s$@$m$&!#e(B  
|  
|e$B$J$s$G%:%l$F$k$s$@$m$&!)e(B e$B;W$$=P$;$J$$!#BhFs0z?t$,;XDj$5$l$Fe(B  
|e$B$$$k;~$K$Oe(B$SAFE=4e$B$G$b0BA4$G$O$J$$$+$H;W$C$?5-21$O$"$k$s$G$9e(B  
|e$B$1$I!#e(B

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://www.ruby-forum.com/u/Hidetoshi_NAGAI)\
**Post date:** [May 29, 2006, 11:21am UTC](https://www.ruby-forum.com/t/load-to-file-mod/60262/8 "2006-05-29T11:21:24Z")

</div>

e$B@DLZ$G$9!#e(B

In mail “[ruby-dev:28670] Re: load\_to(file, mod)”  
Hidetoshi NAGAI [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> e$B1J0fe(B@e$BCNG=!%6e9)Bg$G$9!%e(B

> > mod.module\_eval(File.read(path)) e$B$G!"$@$$$?$$F1$8$3$H$,$G$-$^$9!#e(B

> e$B$3$l$re(B $SAFE == 4 e$B$G$Ne(B load(path, true) e$B$NBe$j$H$7$F;H$\*$&$H$9$k$H!$e(B  
> File.read e$B$,0z$C3]$+$C$A$c$$$^$9$h$M!)e(B

$SAFE=4 e$B$Ge(B load e$B$C$F$G$-$k$s$G$7$?$C$1!#e(B  
e$B$J$s$+$$$+$K$b%@%a$=$&$J$s$G$9$1$I!#e(B

~/c/osm % cat t  
$SAFE = 4  
load “/tmp/a.rb”, true

~/c/osm % cat /tmp/a.rb  
C = “OK”

~/c/osm % forall-ruby t  
ruby 1.8.0 (2003-08-04) [x86\_64-linux]  
ruby 1.8.1 (2003-12-25) [x86\_64-linux]  
ruby 1.8.2 (2004-12-25) [x86\_64-linux]  
ruby 1.8.4 (2005-12-24) [x86\_64-linux]  
ruby 1.8.4 (2006-05-27) [x86\_64-linux]  
ruby 1.9.0 (2006-05-27) [x86\_64-linux]  
t:2:in `load’: Insecure operation - load (SecurityError)  
from t:2

1.8 e$B$@$H$G$-$k$s$G$9$M$(!#$I$C$A$,@5$7$$$s$@$m$&!#e(B
