I'd like to save some typing and do:
--------- code ---------
module LongModuleName
def LongModuleName.foo
puts "..."
end
end
# I don't want to have to keep typing out
# LongModuleName.foo, so let's make a shortcut.
alias foo LongModuleName.foo
foo
--------- /code ---------
But Ruby doesn't like that alias line. Any way to get this nice
shortcut to work?
I'd like to save some typing and do:
--------- code ---------
module LongModuleName
def LongModuleName.foo
You can save some typing here by doing
def self.foo
This is also safer with regard to changes in the module's name.
puts "..."
end
end
# I don't want to have to keep typing out
# LongModuleName.foo, so let's make a shortcut.
alias foo LongModuleName.foo
foo
--------- /code ---------
But Ruby doesn't like that alias line. Any way to get this nice
shortcut to work?
def foo() LogModuleName.foo end
robert
···
2006/7/5, John Gabriele <jmg3000@gmail.com>:
--
Have a look: Robert K. | Flickr
Forgive me if I'm missing something obvious, by why not just use:
def foo;LongName.foo;end
Regards,
Sean
···
On 7/5/06, John Gabriele <jmg3000@gmail.com> wrote:
I'd like to save some typing and do:
--------- code ---------
module LongModuleName
def LongModuleName.foo
puts "..."
end
end
# I don't want to have to keep typing out
# LongModuleName.foo, so let's make a shortcut.
alias foo LongModuleName.foo
foo
--------- /code ---------
But Ruby doesn't like that alias line. Any way to get this nice
shortcut to work?
Redefining the method should work (as long as I pass all args along to
the fully-qualified call). Thank you. Hadn't thought of that.
But it turns out that, unfortunately, I'm more confused now than
before... The module in question is an extension module (written in
C). I'd thought that it was just a pile of *module* methods (like
"foo" is in the above example). But, for some reason, if I do:
include LongModuleName
foo
it actually works.
The calls in the extension module code are set up using
rb_define_module_function(...).
It doesn't look like there are any instance methods in this module:
irb:> LongModuleName.instance_methods
==>
(Hm.. Why no Module.module_methods method?)
Yet, still, inexplicably, I'm able to "include LongModuleName" and
then just call the methods without prefixing them with the modules's
name.
Maybe this has turned into the subject of another post.
Thanks.
---John
···
On 7/5/06, Sean O'Halpin <sean.ohalpin@gmail.com> wrote:
On 7/5/06, John Gabriele <jmg3000@gmail.com> wrote:
> I'd like to save some typing and do:
>
> --------- code ---------
> module LongModuleName
> def LongModuleName.foo
> puts "..."
> end
> end
>
> # I don't want to have to keep typing out
> # LongModuleName.foo, so let's make a shortcut.
> alias foo LongModuleName.foo
>
> foo
> --------- /code ---------
>
> But Ruby doesn't like that alias line. Any way to get this nice
> shortcut to work?
Forgive me if I'm missing something obvious, by why not just use:
def foo;LongName.foo;end
Regards,
Sean
Ok. I see. Object#methods.
LongModuleName.methods show's em all. So, they seem to be all "module
methods" (rather than instance methods).
···
On 7/5/06, John Gabriele <jmg3000@gmail.com> wrote:
[snip]
It doesn't look like there are any instance methods in this module:
irb:> LongModuleName.instance_methods
==>
(Hm.. Why no Module.module_methods method?)
Indeed, it has. Started a new thread with the subject, "module methods
acting like instance methods in extension module"
Thanks,
---John
···
On 7/5/06, I wrote:
[snip]
Yet, still, inexplicably, I'm able to "include LongModuleName" and
then just call the methods without prefixing them with the modules's
name.
Maybe this has turned into the subject of another post.