selective module include

B

Bill Kelly

Howdy!

I'm inheriting from BasicObject (ruby 1.9), but in my case
I'd like it to be slightly less basic.

Currently I'm importing methods like:

def lambda(*args, &block)
Kernel.lambda(*args, &block)
end

def raise(*args)
Kernel.raise(*args)
end

def throw(*args)
Kernel.throw(*args)
end

But I'm guessing there's probably a way I could do more
of a selective 'include' and import these methods more
directly, rather than wrapping them like this?

(I looked at the source for rb_include_module and
include_class_new, but it didn't seem to involve any
loops that operated on a method-by-method basis.)


Thanks for any help,

Bill
 
M

Michael Guterl

Howdy!

I'm inheriting from BasicObject (ruby 1.9), but in my case
I'd like it to be slightly less basic.

Currently I'm importing methods like:

=C2=A0def lambda(*args, &block)
=C2=A0 Kernel.lambda(*args, &block)
=C2=A0end

=C2=A0def raise(*args)
=C2=A0 Kernel.raise(*args)
=C2=A0end
=C2=A0def throw(*args)
=C2=A0 Kernel.throw(*args)
=C2=A0end

But I'm guessing there's probably a way I could do more of a selective
'include' and import these methods more
directly, rather than wrapping them like this?

(I looked at the source for rb_include_module and
include_class_new, but it didn't seem to involve any
loops that operated on a method-by-method basis.)

You may want to look at Daniel Berger's "use" library. Although I
cannot find it anywhere, he does mention it in ruby-talk:316199 and
includes the source code in the message.

http://www.nabble.com/Help,-Ruby-1.8.6-p287-broke-my-"use"--library-and=
-I-don%27t-know-why-td19706284.html

Best regards,
Michael Guterl
 
B

Bill Kelly

From: "Michael Guterl said:
You may want to look at Daniel Berger's "use" library. Although I
cannot find it anywhere, he does mention it in ruby-talk:316199 and
includes the source code in the message.

http://www.nabble.com/Help,-Ruby-1....-library-and-I-don't-know-why-td19706284.html

Thanks! That got me started. I ended up with the following, which seems
to behave as I'd hoped.

class SpartanObject < BasicObject
include ::Kernel

keep = BasicObject.public_instance_methods +
BasicObject.protected_instance_methods +
BasicObject.private_instance_methods +
%w(
catch class inspect object_id raise throw to_s
is_a? kind_of? nil? respond_to?
).map {|m| m.to_sym}

(public_instance_methods +
protected_instance_methods +
private_instance_methods).each {|m| undef_method m unless keep.include? m}
end


Regards,

Bill
 

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,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top