How do you call a global method from a module method where there is another
module method that shares the name of the global method?
Sean O'Dell
How do you call a global method from a module method where there is another
module method that shares the name of the global method?
Sean O'Dell
Perhaps that was too cryptic. Here’s an example:
def test
p “in first test”
end
module somemodule
def somemodule::somemethod
test()
end
def somemodule::test
p “in somemodule::test”
end
end
In somemodule::somemethod, I intended to call the global test method, but I
ended up calling somemodule::test.
How do you call the outer global method in a case like this?
Sean O'Dell
On Thursday 27 May 2004 16:37, Sean O’Dell wrote:
How do you call a global method from a module method where there is another
module method that shares the name of the global method?
HI –
How do you call a global method from a module method where there is another
module method that shares the name of the global method?Perhaps that was too cryptic. Here’s an example:
def test
p “in first test”
endmodule somemodule
def somemodule::somemethod
test()
enddef somemodule::test
p “in somemodule::test”
end
endIn somemodule::somemethod, I intended to call the global test method, but I
ended up calling somemodule::test.How do you call the outer global method in a case like this?
I’m not sure this is the slickest way… but you could do:
def m
1
end
public :m
class C
def C.m
2
end
def C.n
Object.m # or [].m or "hi".m, for that matter :-)
end
end
p C.n # 1
David
On Fri, 28 May 2004, Sean O’Dell wrote:
On Thursday 27 May 2004 16:37, Sean O’Dell wrote:
–
David A. Black
dblack@wobblini.net
I might expect (did expect, as a matter of fact) to be able to call
such a method just by doing the following:
def somemodule::somemethod
::test()
end
That is, prefix the method name by a double colon as you would a
constant defined outside of any module [1]. It doesn’t work, though.
Some mechanism like that would be awfully useful in certain cases (I’ve
personally come across such a case in a real application), and could be
implemented much less hackishly than Ruby currently allows.
Specifically, what’s needed is a way to access the top-level “module”
within which methods are defined (or are they defined in Object by
default?).
Failing that, see previous replies.
[1] See “The Ruby Language” chapter in Pickaxe, section named “Scope of
Constants and Variables.”
On May 27, 2004, at 7:24 PM, Sean O’Dell wrote:
On Thursday 27 May 2004 16:37, Sean O’Dell wrote:
How do you call a global method from a module method where there is
another
module method that shares the name of the global method?Perhaps that was too cryptic. Here’s an example:
def test
p “in first test”
endmodule somemodule
def somemodule::somemethod
test()
enddef somemodule::test
p “in somemodule::test”
end
endIn somemodule::somemethod, I intended to call the global test method,
but I
ended up calling somemodule::test.How do you call the outer global method in a case like this?
Sean O’Dell
!DSPAM:40b686e1121078289412664!
Hi,
On Thursday 27 May 2004 16:37, Sean O’Dell wrote:
How do you call a global method from a module method where there is another
module method that shares the name of the global method?Perhaps that was too cryptic. Here’s an example:
def test
p “in first test”
endmodule somemodule
def somemodule::somemethod
test()
enddef somemodule::test
p “in somemodule::test”
end
endIn somemodule::somemethod, I intended to call the global test method, but I
ended up calling somemodule::test.
Not super pretty, but I’m currently using:
def somemodule::somemethod
Kernel.instance_eval { test() }
end
Regards,
Bill
That’s pretty ugly. I figured it would come down to having to do something
icky like this. Bah.
There really should be a module called global, or perhaps an object named
global, that keeps all the global variables and methods. That way you could
just call global.method. There should also be syntax sugar to allow you to
say ::method.
Sean O'Dell
On Thursday 27 May 2004 22:52, Bill Kelly wrote:
Not super pretty, but I’m currently using:
def somemodule::somemethod
Kernel.instance_eval { test() }
end
There really should be a module called global, or perhaps an object named
global, that keeps all the global variables and methods. That way you could
just call global.method. There should also be syntax sugar to allow you to
say ::method.
"global" methods are private : this is why you can't call Object.test
svg% ruby -e 'def test() end; p Object.test'
-e:1: private method `test' called for Object:Class (NoMethodError)
svg%
Guy Decoux
Hi,
Not super pretty, but I’m currently using:
def somemodule::somemethod
Kernel.instance_eval { test() }
endThat’s pretty ugly. I figured it would come down to having to do something
icky like this. Bah.
How 'bout:
class Global
def self.method_missing(meth_id, *args)
Object.instance_eval { self.send(meth_id, *args) }
end
end
Then we can do:
def somemodule::somemethod
Global.test # or Global::test
end
Regards,
Bill
Yes, but that’s not how globals usually behave. Globals are usually always
globally available unless overridden or removed.
Sean O'Dell
On Friday 28 May 2004 10:41, ts wrote:
There really should be a module called global, or perhaps an object
named S> global, that keeps all the global variables and methods. That way
you could S> just call global.method. There should also be syntax sugar to
allow you to S> say ::method.“global” methods are private : this is why you can’t call Object.test
svg% ruby -e ‘def test() end; p Object.test’
-e:1: private method `test’ called for Object:Class (NoMethodError)
svg%
I think it should be something built-into the language, so while this is much
nicer, it’s still not something I’m happy with.
Sean O'Dell
On Friday 28 May 2004 13:57, Bill Kelly wrote:
From: “Sean O’Dell” sean@celsoft.com
On Thursday 27 May 2004 22:52, Bill Kelly wrote:
Not super pretty, but I’m currently using:
def somemodule::somemethod
Kernel.instance_eval { test() }
endThat’s pretty ugly. I figured it would come down to having to do
something icky like this. Bah.How 'bout:
class Global
def self.method_missing(meth_id, *args)
Object.instance_eval { self.send(meth_id, *args) }
end
endThen we can do:
def somemodule::somemethod
Global.test # or Global::test
end