Anonymous module undefined method error

J

johng

Hi all

I am new to Ruby so please excuse my question. I did search
usenet for an answer but couldn't find one:

I have a variable, data, that was read from a file and has the
following contents:

def foo
return "foo"
end

My code is this:

amodule = Module.new
amodule.class_eval(data)

puts(amodule.public_instance_methods) -> outputs "foo"

amodule.foo -> fails saying method is
undefined


If I change data to have a class in it with a class method, I can
call it just fine like so:

amodule::Test.foo

Any help very much appreciated.

John.
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Anonymous module undefined method error"

|I am new to Ruby so please excuse my question. I did search
|usenet for an answer but couldn't find one:
|
|I have a variable, data, that was read from a file and has the
|following contents:
|
|def foo
| return "foo"
|end
|
|My code is this:
|
|amodule = Module.new
|amodule.class_eval(data)
|
|puts(amodule.public_instance_methods) -> outputs "foo"
|
|amodule.foo -> fails saying method is undefined

A public *instance* method means methods are available for their
instances, "amodule" is a module, not an instance. When you include a
module to a class, and instantiate from the class, you can call "foo".

matz.
 
J

johng

Hi,

In message "Re: Anonymous module undefined method error"
|

I am new to Ruby so please excuse my question. I did search
|usenet for an answer but couldn't find one:
|
|I have a variable, data, that was read from a file and has the
|following contents:
|
|def foo
| return "foo"
|end
|
|My code is this:
|
|amodule = Module.new
|amodule.class_eval(data)
|
|puts(amodule.public_instance_methods) -> outputs "foo"
|
|amodule.foo -> fails saying method is
undefined

|A public *instance* method means methods are available for their
|instances, "amodule" is a module, not an instance. When you include a
|module to a class, and instantiate from the class, you can call "foo".
|
| matz.

Ah, ok, I was fooled by the Module.new. I had assumed that it made
an instance.

So, what I wanted to do, was have a class defined in a file and call
a known method on it. For instance:

class Bar
def Bar.foo
return "foo"
end
end

But I will not know the class name contained in the file (Bar). I
tried
the approach from the original post so that I could call a defined
method that would instantiate the class for me. I believe I can do
this now relatively easily. It does though put more requirements
on the file.

I wonder though, if there is a cleaner way?

Thanks,

John.
 
S

Shad Sterling

Hi,
=20
In message "Re: Anonymous module undefined method error"
=20
|
=20
I am new to Ruby so please excuse my question. I did search
|usenet for an answer but couldn't find one:
|
|I have a variable, data, that was read from a file and has the
|following contents:
|
|def foo
| return "foo"
|end
|
|My code is this:
|
|amodule =3D Module.new
|amodule.class_eval(data)
|
|puts(amodule.public_instance_methods) -> outputs "foo"
|
|amodule.foo -> fails saying method is
undefined
=20
|A public *instance* method means methods are available for their
|instances, "amodule" is a module, not an instance. When you include a
|module to a class, and instantiate from the class, you can call "foo".
|
| matz.
=20
Ah, ok, I was fooled by the Module.new. I had assumed that it made
an instance.
=20
So, what I wanted to do, was have a class defined in a file and call
a known method on it. For instance:
=20
class Bar
def Bar.foo
return "foo"
end
end
=20
But I will not know the class name contained in the file (Bar). I
tried
the approach from the original post so that I could call a defined
method that would instantiate the class for me. I believe I can do
this now relatively easily. It does though put more requirements
on the file.
=20
I wonder though, if there is a cleaner way?
=20
Thanks,
=20
John.
=20
=20
=20


more requirements on the file? you can do the same thing, you just
have to create an instance:

-bash: [~]$ cat testdata
def foo
"foo!"
end

-bash: [~]$ cat test.rb
data =3D File.read( "testdata" )
aclass =3D Class.new
aclass.class_eval( data )
a =3D aclass.new
puts a.foo

-bash: [~]$ ruby test.rb
foo!




--=20
 
J

Joel VanderWerf

johng wrote:
...
So, what I wanted to do, was have a class defined in a file and call
a known method on it. For instance:

class Bar
def Bar.foo
return "foo"
end
end

But I will not know the class name contained in the file (Bar). I
tried
the approach from the original post so that I could call a defined
method that would instantiate the class for me. I believe I can do
this now relatively easily. It does though put more requirements
on the file.

This may be of use to you:

http://raa.ruby-lang.org/project/script/

It's a very simple but useful way to load a ruby script and access the
methods, classes, and other constants defined in it.
 
J

johng

more requirements on the file? you can do the same thing, you just
have to create an instance:

-bash: [~]$ cat testdata
def foo
"foo!"
end

-bash: [~]$ cat test.rb
data = File.read( "testdata" )
aclass = Class.new
aclass.class_eval( data )
a = aclass.new
puts a.foo

-bash: [~]$ ruby test.rb
foo!

I was able to get this to work great. I would like to have testdata
contain a complete class definition (including base classes) and
be able to call methods on it without knowing its name.

Is there some way for the class inside testdata to "register" itself
with
the anonymous class instance "a"? I thought that I could just get the
class to call a method and "a" would respond as it is the superclass.
That didn't seem to work.

Any help/pointers very much appreciated.

John.
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top