[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Equivalent of "source" in ruby?

Todd A. Jacobs

9/7/2007 5:17:00 AM

Contrary to my expectations, if I have some variable assignments in an
external file, I can't simply call:

load rcfile

to have it sourced. Instead, I found myself having to do this:

IO.foreach(rcfile) do |line|
eval line
end

which seems lame. Is there a better way?

--
"Oh, look: rocks!"
-- Doctor Who, "Destiny of the Daleks"

9 Answers

Alex Gutteridge

9/7/2007 5:28:00 AM

0

On 7 Sep 2007, at 14:17, Todd A. Jacobs wrote:

> Contrary to my expectations, if I have some variable assignments in an
> external file, I can't simply call:
>
> load rcfile
>
> to have it sourced. Instead, I found myself having to do this:
>
> IO.foreach(rcfile) do |line|
> eval line
> end
>
> which seems lame. Is there a better way?

Would globals work for you?

[alexg@powerbook]/Users/alexg/Desktop(28): cat load_me.rb
$global=10
local=20
[alexg@powerbook]/Users/alexg/Desktop(29): cat main.rb
load 'load_me.rb'

puts $global
puts local
[alexg@powerbook]/Users/alexg/Desktop(30): ruby main.rb
10
main.rb:4: undefined local variable or method `local' for main:Object
(NameError)

Alex Gutteridge

Bioinformatics Center
Kyoto University



Michael T. Richter

9/7/2007 5:39:00 AM

0

On Fri, 2007-07-09 at 14:17 +0900, Todd A. Jacobs wrote:

> Contrary to my expectations, if I have some variable assignments in an
> external file, I can't simply call:
>
> load rcfile
>
> to have it sourced. Instead, I found myself having to do this:
>
> IO.foreach(rcfile) do |line|
> eval line
> end
>
> which seems lame. Is there a better way?



#test1.rb:

@a = 5
@b = 4

require 'test2'

p @a
p @b
p @c

#----8<-----

#test2.rb:

@b = 3
@c = 2


$ ruby test1.rb
5
3
2
$

Or am I missing something?
--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
So much of what we call management consists in making it difficult for
people to work. (Peter Drucker)

Erik Veenstra

9/7/2007 10:50:00 AM

0

> Contrary to my expectations, if I have some variable
> assignments in an external file, I can't simply call:
>
> load rcfile

Load starts a new scope without reusing the local scope. Eval
starts a new scope with reusing the local scope. So, use eval
instead. But you should "define" the vars before loading them.

gegroet,
Erik V. - http://www.erikve...

----------------------------------------------------------------

$ cat test.rb
a=nil
b=nil

eval(File.read("vars.rb"))

# Or better: Thread.new{$SAFE=4 ; eval(File.read("vars.rb"),
Module.new.instance_eval{binding})}.join

p a
p b

----------------------------------------------------------------

$ cat vars.rb
a = 7
b = 8

----------------------------------------------------------------

$ ruby test.rb
7
8

----------------------------------------------------------------


Robert Klemme

9/7/2007 10:53:00 AM

0

2007/9/7, Michael T. Richter <ttmrichter@gmail.com>:
>
> On Fri, 2007-07-09 at 14:17 +0900, Todd A. Jacobs wrote:
> Contrary to my expectations, if I have some variable assignments in an
> external file, I can't simply call:
>
> load rcfile
>
> to have it sourced. Instead, I found myself having to do this:
>
> IO.foreach(rcfile) do |line|
> eval line
> end
>
> which seems lame. Is there a better way?
>
>
> #test1.rb:
>
> @a = 5
> @b = 4
>
> require 'test2'
>
> p @a
> p @b
> p @c
> #----8<-----
>
> #test2.rb:
>
> @b = 3
> @c = 2
>
> $ ruby test1.rb
> 5
> 3
> 2
> $
>
> Or am I missing something?

Yes, you are missing the local variable bit. :-)

robert

Robert Klemme

9/7/2007 10:55:00 AM

0

2007/9/7, Todd A. Jacobs <tjacobs-sndr-019fdb@codegnome.org>:
> Contrary to my expectations, if I have some variable assignments in an
> external file, I can't simply call:
>
> load rcfile
>
> to have it sourced. Instead, I found myself having to do this:
>
> IO.foreach(rcfile) do |line|
> eval line
> end
>
> which seems lame. Is there a better way?

It is also unsafe - not only because of the eval but also because this
will give errors for expressions that span multiple lines. The easy
fix would be

eval(File.read(rc_file))

But I'd rather resort to one of the other suggestions (namely using
local variables).

Kind regards

robert

Erik Veenstra

9/7/2007 10:56:00 AM

0

a = nil
b = nil

Thread.new do
data = File.read("vars.rb")
$SAFE = 4
b = Module.new.instance_eval{binding}

eval(data, b)
end.join

p a
p b


Michael T. Richter

9/7/2007 12:21:00 PM

0

On Fri, 2007-07-09 at 19:53 +0900, Robert Klemme wrote:

> Yes, you are missing the local variable bit. :-)


That would do it. :D

--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
When debugging, novices insert corrective code; experts remove defective
code. (Richard Pattis)

Eric Hodel

9/10/2007 4:23:00 AM

0

On Sep 7, 2007, at 03:55, Erik Veenstra wrote:

> a = nil
> b = nil
>
> Thread.new do
> data = File.read("vars.rb")
> $SAFE = 4
> b = Module.new.instance_eval{binding}
>
> eval(data, b)
> end.join
>
> p a
> p b

echo "Thread.critical = true; sleep" >> vars.rb

--
Poor workers blame their tools. Good workers build better tools. The
best workers get their tools to do the work for them. -- Syndicate Wars



Robert Klemme

9/10/2007 7:00:00 AM

0

2007/9/7, Robert Klemme <shortcutter@googlemail.com>:
> 2007/9/7, Todd A. Jacobs <tjacobs-sndr-019fdb@codegnome.org>:
> > Contrary to my expectations, if I have some variable assignments in an
> > external file, I can't simply call:
> >
> > load rcfile
> >
> > to have it sourced. Instead, I found myself having to do this:
> >
> > IO.foreach(rcfile) do |line|
> > eval line
> > end
> >
> > which seems lame. Is there a better way?
>
> It is also unsafe - not only because of the eval but also because this
> will give errors for expressions that span multiple lines. The easy
> fix would be
>
> eval(File.read(rc_file))
>
> But I'd rather resort to one of the other suggestions (namely using
> local variables).

That was intended to read "global" of course. Sorry for the noise.

robert