Can I alias foo --> LongModuleName.foo?

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?

2006/7/5, John G. [email protected]:

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.

But Ruby doesn’t like that alias line. Any way to get this nice
shortcut to work?

def foo() LogModuleName.foo end

robert

On 7/5/06, John G. [email protected] wrote:

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

On 7/5/06, John G. [email protected] 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?)

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, 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.

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, Sean O’Halpin [email protected] wrote:

I don’t want to have to keep typing out

def foo;LongName.foo;end

Regards,
Sean

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs