Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alias a method multiple times

I wish to put two aliases for one original method, but i don't see the ability of alias_method to do multiple aliases at once, rather one by one.

So is there a possibility to change from this:

alias_method :aliased_first_method, :first_method
alias_method :aliased_first_method?, :first_method

to something like this:

alias_method [:aliased_first_method, :aliased_first_method?], :first_method

I'm not interested in creating custom methods.

like image 933
Zippie Avatar asked Nov 14 '13 13:11

Zippie


2 Answers

I do not think there is a better way than just using each:

[:aliased_first_method, :aliased_first_method?].each{|ali| alias_method ali, :first_method}

Edit 2021 Ruby 2.7+

%i[aliased_first_method aliased_first_method?].each{ alias_method _1, :first_method]}
like image 176
hirolau Avatar answered Sep 20 '22 14:09

hirolau


Looking at the docs and source of alias_method, I would say that what you want is not possible without a custom method.

(Just had to answer my almost namesake :))

like image 21
zwippie Avatar answered Sep 21 '22 14:09

zwippie