[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Anonymous module undefined method error

johng

5/20/2005 9:42:00 PM

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.

7 Answers

Yukihiro Matsumoto

5/21/2005 3:06:00 AM

0

Hi,

In message "Re: Anonymous module undefined method error"
on Sat, 21 May 2005 06:45:15 +0900, "johng" <jgoalby@gmail.com> writes:

|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.


johng

5/21/2005 3:38:00 AM

0

Hi,

In message "Re: Anonymous module undefined method error"
on Sat, 21 May 2005 06:45:15 +0900, "johng" <jgoa...@gmail.com>
writes:

|

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.

Shad Sterling

5/21/2005 6:22:00 AM

0

On 5/20/05, johng <jgoalby@gmail.com> wrote:
> Hi,
>
> In message "Re: Anonymous module undefined method error"
> on Sat, 21 May 2005 06:45:15 +0900, "johng" <jgoa...@gmail.com>
> writes:
>
> |
>
> 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.
>
>
>


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!




--

----------

Please do not send personal (non-list-related) mail to this address.
Personal mail should be sent to polyergic@sterfish.com.


johng

5/21/2005 7:50:00 PM

0

Thankyou Shad!

John.

Joel VanderWerf

5/21/2005 8:06:00 PM

0

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/proje...

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


johng

5/22/2005 10:02:00 PM

0

> This may be of use to you:

> http://raa.ruby-lang.org/proje...

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

Great, I will take a look.
John.

johng

5/25/2005 5:44:00 AM

0

> 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.