Loading singletons from a file.

T

Travis Smith

Say I have a class Here, and I loaded that class from some file
outside my driver via require. So now the class Here is in my
namespace and I can create a new one with Here.new. What I would like
to do is create a singleton of the same class with information stored
in another ruby file. That file would contain something like this....

class Here
def initialize
@var1 = "something else"
end

# adding a method that isn't part of the normal Here class
def new_method
# do something
end
end

Now, I could just create the singleton and store it somewhere for when
I needed it, but I plan on having a number of these. Each of them in
their own file. I thought perhaps I would need to add a line of code
to the end of the to store that singleton where I needed it. This
really didn't end up working for me. It would just use the global
class when I would load the file via load _filename_, false.

What I would really like is just some way to turn a file into an
anonymous class and be able to initialize it just as is. I'm not sure
if that's easily possible though.

Thanks for any ideas here. I've only been on Ruby for four days now,
but it's been pretty easy to get this far.
 
R

Robert Klemme

Travis Smith said:
Say I have a class Here, and I loaded that class from some file
outside my driver via require. So now the class Here is in my
namespace and I can create a new one with Here.new. What I would like
to do is create a singleton of the same class with information stored
in another ruby file. That file would contain something like this....

class Here
def initialize
@var1 = "something else"
end

# adding a method that isn't part of the normal Here class
def new_method
# do something
end
end

Now, I could just create the singleton and store it somewhere for when
I needed it, but I plan on having a number of these. Each of them in
their own file. I thought perhaps I would need to add a line of code
to the end of the to store that singleton where I needed it. This
really didn't end up working for me. It would just use the global
class when I would load the file via load _filename_, false.

What I would really like is just some way to turn a file into an
anonymous class and be able to initialize it just as is. I'm not sure
if that's easily possible though.

Have a look at the attached files and see whether that helps you.
Thanks for any ideas here. I've only been on Ruby for four days now,
but it's been pretty easy to get this far.

:)

Kind regards

robert
 
T

Travis Smith

Turns out the reason it didn't work at all for me was that my class
was being defined with 'def' instead of 'class'. Silly how typos break
everything...

That's very similar to what I have working now though the @instances
is stored in another class and I truncate the filenames (remove the
rb). I have a class which I 'ask' for the instance I want and if it
doesn't exist it will go out and load it. I think keeping the list of
objects inside the class might be better than my current method...

Well, now I have more to play with. We'll see where takes me. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top