My usage of const_missing and method_missing

J

John Lam

------=_Part_45823_27972356.1131568324949
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I was wondering if folks with much more experience with Ruby than I could
comment on my usage of const_missing and method_missing in this code:

class ClrClass
def method_missing(name)
self.class.class_eval do
define_method(name) { name.to_s } # TODO: I will dynamically create IL
shims here
@method_count +=3D 1
end
self.method(name).call
end
def get_dotnet_type
self.class.instance_variable_get:)@dotnet_type)
end
def get_method_count
self.class.instance_variable_get:)@method_count)
end
end

class Module
def const_missing(name)
# TODO: I'll lookup the CLR type by prepending names that I find in the
ancestors array - I know that part works already.
c =3D Class.new(ClrClass)
c.instance_variable_set:)@dotnet_type, name)
c.instance_variable_set:)@method_count, 0)
const_set(name, c)
end
end

The current version of my Ruby <-> CLR bridge doesn't use this technique.
I'd like to use this technique to cache all of my generated CIL shims and
CLR Type objects.

Thanks
-John
http://www.iunknown.com


PS Here are the tests that I used:

class MethodMissingTests < Test::Unit::TestCase
def setup
@obj =3D System.new
end

def test_const_missing
assert_equal :System, @obj.get_dotnet_type
end

def test_method_missing
assert_equal "joe", @obj.joe
assert_equal 1, @obj.get_method_count
end

def test_method_proxy_caching
assert_equal "joe", @obj.joe
assert_equal "bob", @obj.bob
assert_equal 2, @obj.get_method_count
assert_equal "joe", @obj.joe
assert_equal 2, @obj.get_method_count
assert_equal "kim", @obj.kim
assert_equal 3, @obj.get_method_count
end
end

------=_Part_45823_27972356.1131568324949--
 
D

Dave Burt

John Lam:
I was wondering if folks with much more experience with Ruby than I could
comment on my usage of const_missing and method_missing in this code:

It's nice, but is there a way to reflect on the .NET target and get at least
the methods all-at-once rather than using method missing? That way you have
the advantage of respond_to? etc. rather than having to refer use a method
first.

Cheers,
Dave
 
J

John Lam

------=_Part_17801_10009872.1131660650321
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
It's nice, but is there a way to reflect on the .NET target and get at
least
the methods all-at-once rather than using method missing? That way you
have
the advantage of respond_to? etc. rather than having to refer use a metho= d
first.

I could get the methods all at once, but I use method_missing to determine
which shims I should dynamically construct. Each shim carries some amount o=
f
overhead associated with it in terms of both time and space. I'd like to
avoid constructing shims for methods that are never called.

Cheers,
-John
http://www.iunknown.com

------=_Part_17801_10009872.1131660650321--
 
D

Duane Johnson

I could get the methods all at once, but I use method_missing to
determine
which shims I should dynamically construct. Each shim carries some
amount of
overhead associated with it in terms of both time and space. I'd
like to
avoid constructing shims for methods that are never called.
I wonder if you could override respond_to? and tell it to return
'true' if the method exists virtually.

Duane Johnson
(canadaduane)
 
D

Dave Burt

Duane Johnson:
I wonder if you could override respond_to? and tell it to return 'true'
if the method exists virtually.

And even create the shim at that point - who calls respond_to? without then
moving on to call the method?

Cheers,
Dave
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top