[Q] pass local variables to loaded file

Using ‘load()’, current local variables are not
passed to new file.

data.rb
…--------------------
a = 10
b = 20
load ‘view.rb’
…--------------------

view.rb
…--------------------
print “a=#{a}, b=#{b}\n” # ==> ERROR!
…--------------------

…====================
$ ruby data.rb
…/view.rb:1: undefined local variable or method a' for #<Object:0x40299c90> (NameError) from data.rb:3:inload’
from data.rb:3
…====================

I want to refer local variables ‘a’ and ‘b’ from view.rb.
Could you give me a good solution?

kwatch

Hi,

I want to refer local variables ‘a’ and ‘b’ from view.rb.
Could you give me a good solution?

No. How about

view.rb:

def init_view(a,b)

end

load “view.rb”
init_view(10,20)

?

						matz.
···

In message “[Q] pass local variables to loaded file” on 03/02/18, kwatch kwatch@lycos.jp writes:

matz@ruby-lang.org (Yukihiro Matsumoto) wrote in message news:1045548557.675332.1059.nullmailer@picachu.netlab.jp

Hi,

I want to refer local variables ‘a’ and ‘b’ from view.rb.
Could you give me a good solution?

No. How about

view.rb:

def init_view(a,b)

end

load “view.rb”
init_view(10,20)

?

  					matz.

Thanks matz.

Background:

I’m using ERuby, and separating logic and view.

logic.rb
…--------------------
a = 10
b = 20
require ‘eruby’
ERuby.import(‘view.rhtml’)
…--------------------

view.rhtml
…--------------------

a = <%=a%>, b=<%=b%> ..--------------------

This works very well.

But I think that it must be more fast if I compile eRuby
in advance.

logic.rb
…--------------------
a = 10
b = 20
load ‘view.rb’
…--------------------

view.rb
…--------------------
print “\n”
print " \n"
print " a = “; print((a)); print “, b=”; print((b)); print “\n”
print " \n”
print “\n”
…--------------------

But I got the following error:
./view.rb:3: undefined local variable or method `a’
for #Object:0x40299c90 (NameError)

To create a function is not difficult,
but it is difficult to create a function automatically,
because it is needed to retrieve local variables automatically
when creating a function.

…--------------------
def print_view(a, b) # retrieve local vars
print “\n”
print " \n"
print " a = “; print((a)); print “, b=”; print((b)); print “\n”
print " \n”
print “\n”
end
…--------------------

Hmm…
I’ll use ERuby.import() meekly.

···

In message “[Q] pass local variables to loaded file” > on 03/02/18, kwatch kwatch@lycos.jp writes:


regards
kwatch