Inconsistent(?) when including modules

P

Paulo Jabardo

I was playing around with modules when I noticed that
I counld, for example include Math and use sin,cos,etc
as if it were a common method. When I tried to do the
same in irb

module Test
def Test.triple(x)
3*x
end
end

Test.triple 2 =3D> 6

If I include Test from the toplevel
inlcude Test

and then call triple directly I get an error:
irb(main):005:0> triple 2
NoMethodError: undefined method `triple' for
main:Object
from (irb):5


I then tried to implement the same module as an
extension:

#include <ruby.h>

static VALUE triple(VALUE self, VALUE x){
return rb_float_new(3.0*NUM2DBL(x));
}

VALUE modulo;

void Init_TestC(){
modulo =3D rb_define_module("TestC");
rb_define_module_function(modulo, "triple", triple,
1);
}
=20
When I load this extension,
require 'TestC.so'

TestC.triple 2 =3D> 6

include TestC
triple 2 =3D> 6 # No error

I haven't seen this documented. Can I get this same
behaviour from ruby code or do I need to write an
extension?

Thanks

Paulo Jabardo


=09
=09
=09
_______________________________________________________=20
Yahoo! Acesso Gr=E1tis - Internet r=E1pida e gr=E1tis.=20
Instale o discador agora! http://br.acesso.yahoo.com/
 
J

Jason Foreman

I was playing around with modules when I noticed that
I counld, for example include Math and use sin,cos,etc
as if it were a common method. When I tried to do the
same in irb
=20
module Test
def Test.triple(x)
3*x
end
end
=20
Test.triple 2 =3D> 6
=20
If I include Test from the toplevel
inlcude Test
=20
and then call triple directly I get an error:
irb(main):005:0> triple 2
NoMethodError: undefined method `triple' for
main:Object
from (irb):5
=20
=20
I then tried to implement the same module as an
extension:
=20
#include <ruby.h>
=20
static VALUE triple(VALUE self, VALUE x){
return rb_float_new(3.0*NUM2DBL(x));
}
=20
VALUE modulo;
=20
void Init_TestC(){
modulo =3D rb_define_module("TestC");
rb_define_module_function(modulo, "triple", triple,
1);
}
=20
When I load this extension,
require 'TestC.so'
=20
TestC.triple 2 =3D> 6
=20
include TestC
triple 2 =3D> 6 # No error
=20
I haven't seen this documented. Can I get this same
behaviour from ruby code or do I need to write an
extension?
=20


You ruby module and extension module are not equivalent. Try instead this:

module Test
def triple(x)
3*x
end
module_function :triple
end

Test.triple 2 # =3D> 6

include Test
triple 2 # =3D> 6



Jason
 
R

Robert Klemme

Jason said:
You ruby module and extension module are not equivalent. Try instead
this:

module Test
def triple(x)
3*x
end
module_function :triple
end

Test.triple 2 # => 6

include Test
triple 2 # => 6

It's even simpler:

module Test
def triple(x)
3*x
end
end
=> 6

Kind regards

robert
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top