Tom Werner
12/13/2006 7:49:00 PM
Scott Strattner wrote:
> Looking at the examples in the book (p. 408 in 2nd edition), it appears
> I can do a <Class>.instance_method(:<method name>) assignment, then bind
> that to a particular object, and then run call. For instance (from
> book):
>
> str = "cat"
> ulen = String.instance_method(:length)
> blen = ulen.bind(str)
> blen.call -> 3
>
> How I'd like to work that in my program is to define two constants
> (within a class called Device), which correspond to the two
> instance_methods:
>
> class Device
> ....
> GET = SMTP::Manager.instance_method(:get)
> WALK = SMTP::Manager.instance_method(:walk)
>
> and then:
>
> def query_device(mibval,type=GET,mib=RFC1213)
> data = []
> response = NOTSET
> begin
> SNMP::Manager.open(:Host => @ip.to_s,
> :Community => @comm) do
> |manager|
> manager.load_module(mib) unless (mib == RFC1213)
> mngr = type.bind(manager)
> response = mngr.call(mibval)
> end
> rescue
> etc.....
> end
>
> But when I try running this, I get the following error:
>
> uninitialized constant Device::SMTP (NameError)
>
> It appears to be adding my class onto the method name. I do have the
> necessary "require" in place (before I define the constants), and the
> SNMP::Manager.open command worked fine (before I make changes to get
> dynamic method calls to work). So I'm not sure why it's adding my class
> name here but it doesn't try to do that under query_device.
>
Try prefixing with ::, that will get you back to the root scope:
GET = ::SMTP::Manager.instance_method(:get)
Tom