Forum: Ruby "load"ing a file and keeping the current scope?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
48124635945b45221ba12a26371f9e3e?d=identicon&s=25 Philip Hallstrom (Guest)
on 2006-05-08 22:08
(Received via mailing list)
Hi -

For a variety of reasons I have the following situation:

---- file_a.rb --------------------------------------
class A
 	... magic loading goes here...
end
--------------------------------------------------

---- file_b.rb --------------------------------------
class B
 	... magic loading goes here...
end
--------------------------------------------------

---- magic_file.rb -----------------------------
... does some stuff...
----------------------------------------------

How can I include the contents of magic_file.rb into both a and b "as
is".
I've tried load() and it changes the scope so that magic_file.rb doesn't
realize it's in A or B.

I guess I'm looking for the PHP include() equivalent.

I realize this is a weird question and I realize in most situations this
is *not* the way to do it, but I don't have a choice... so please no "do
it this other way" solutions :)

Thanks!

-philip
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-08 22:11
(Received via mailing list)
On May 8, 2006, at 4:05 PM, Philip Hallstrom wrote:

> ---- file_b.rb --------------------------------------
> magic_file.rb doesn't realize it's in A or B.
>
>

>

class A
    eval(File.read("magic_file.rb"))
end
A19281bdbc5f08539cdef3d6636f7c4d?d=identicon&s=25 Christoffer Sawicki (Guest)
on 2006-05-08 22:14
(Received via mailing list)
On 5/8/06, Philip Hallstrom <ruby@philip.pjkh.com> wrote:
> How can I include the contents of magic_file.rb into both a and b "as is".
> I've tried load() and it changes the scope so that magic_file.rb doesn't
> realize it's in A or B.
>
> I guess I'm looking for the PHP include() equivalent.
>
> I realize this is a weird question and I realize in most situations this
> is *not* the way to do it, but I don't have a choice... so please no "do
> it this other way" solutions :)

I believe the following is your only choice:

  eval File.read("magic_file.rb")
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-08 22:15
(Received via mailing list)
On May 8, 2006, at 4:08 PM, Logan Capaldo wrote:

>> end
>>
>> Thanks!
>
>
>


I realize this is against your "no other solutions" rule, but why won't

% cat magic_file.rb
module Stuff
   ...
end

% cat a.rb
class A
   require 'magic_file'
   include Stuff
end

work?
2c7c807a1df0c76a8fc823c709b501a9?d=identicon&s=25 Victor Shepelev (Guest)
on 2006-05-08 22:18
(Received via mailing list)
From: Logan Capaldo [mailto:logancapaldo@gmail.com]
> > --------------------------------------------------
> > How can I include the contents of magic_file.rb into both a and b
> > "as is".
> > ...
> >
> > -philip
> >
>
> class A
>     eval(File.read("magic_file.rb"))
> end
>

Interesting solution. What about its effectivenes? And in general,
effectivenes of require'ing something vs. eval'ing it?

Victor
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-08 22:25
(Received via mailing list)
On May 8, 2006, at 4:16 PM, Victor Shepelev wrote:

>>> class A
>>> ---- magic_file.rb -----------------------------
>> class A
>>     eval(File.read("magic_file.rb"))
>> end
>>
>
> Interesting solution. What about its effectivenes? And in general,
> effectivenes of require'ing something vs. eval'ing it?
>
> Victor
>
>

Well require-ing only parses the file once. Other than that, this
takes more memory (since it puts the entire file inside a string) but
other than that I'm pretty sure the difference is effectively zero.
The 'ideal' solution to this problem would be if load took an
optional binding argument
e.g.:

class A
   load("magic_file.rb", false, binding)
end
48124635945b45221ba12a26371f9e3e?d=identicon&s=25 Philip Hallstrom (Guest)
on 2006-05-08 23:24
(Received via mailing list)
>>> 	... magic loading goes here...
>>> I guess I'm looking for the PHP include() equivalent.
>>
>>>
>>
>> class A
>>   eval(File.read("magic_file.rb"))
>> end

That worked. Thanks!

> include Stuff
> end
>
> work?

Not sure... (i'm fairly new to ruby) but I tried it and it doesn't.
Here's specific details on what I'm doing (rails web services):

http://www.ruby-forum.com/topic/65015#new

-philip
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-08 23:49
(Received via mailing list)
On May 8, 2006, at 5:23 PM, Philip Hallstrom wrote:

>>>> --------------------------------------------------
>>>> Thanks!
>> % cat magic_file.rb
>> work?
>
> Not sure... (i'm fairly new to ruby) but I tried it and it doesn't.
> Here's specific details on what I'm doing (rails web services):
>
> http://www.ruby-forum.com/topic/65015#new
>
> -philip
>

% cat magic_stuff.rb
module FindNewerThan
   def self.included(other)
     other.class_eval do
       api_method :find_newer_than,
      :expects => [{:created_at => :datetime}],
      :returns => [[Class.const_get(self.name.to_s.gsub(/Api$/, ''))]]
     end
   end
end

% cat a.rb
require 'magic_stuff.rb'
class A
   include FindNewerThan
end

class B
   include FindNewerThan
end


etc...
This topic is locked and can not be replied to.