From: kwatch@... (kwatch) Date: 2003-02-19T04:29:03+09:00 Subject: Re: [Q] pass local variables to loaded file matz@ruby-lang.org (Yukihiro Matsumoto) wrote in message news:<1045548557.675332.1059.nullmailer@picachu.netlab.jp>... > Hi, > > In message "[Q] pass local variables to loaded file" > on 03/02/18, kwatch writes: > > |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 # (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. -- regards kwatch