Feature #6074

Allow alias arguments to have a comma

Added by Thomas Sawyer about 2 years ago. Updated over 1 year ago.

[ruby-core:42857]
Status:Assigned
Priority:Low
Assignee:Nobuyoshi Nakada
Category:core
Target version:next minor

Description

This is one my biggest pet peeves with Ruby. I am always typing:

alias :foo, :bar

And getting a damn syntax error.

Btw, is there a reason why alias is a keyword and not a method?

History

#1 Updated by Marc-Andre Lafortune about 2 years ago

  • Priority changed from Normal to Low

If that this is your biggest pet peeve, you must really love Ruby!

Just use alias_method instead. The alias keyword is more general and allows other aliases like global variables; also you don't need to give symbols to alias, you can write alias foo bar.

#2 Updated by Thomas Sawyer about 2 years ago

Yes, I have almost exclusively used #alias_method in the past, but it's always seems rather silly to have to use the longer term. And I bet it would be an easy adjustment to support a comma.

Btw, I really dislike using the non-symbol form of alias as it sticks out under syntax highlighters like a sore thumb.

#3 Updated by Yusuke Endoh about 2 years ago

Hello,

2012/2/24 Thomas Sawyer transfire@gmail.com:

Btw, is there a reason why alias is a keyword and not a method?

Just historical reason, I guess. A keyword-style alias was
first introduced, and then alias_method was later introduced
as a reflection API for the feature.

I understand the feeling. No keyword was needed for such a
feature. If matz designed it now, he would implement it as
a method, I believe.

Btw, I really dislike using the non-symbol form of alias as it sticks out under syntax highlighters like a sore thumb.

In this regard, you should blame your syntax highlighter?

--
Yusuke Endoh mame@tsg.ne.jp

#4 Updated by Koichi Sasada about 2 years ago

  • Category set to core
  • Target version set to 2.0.0

I think there are no reason why comma should be rejected.

#5 Updated by Nobuyoshi Nakada about 2 years ago

Hi,

(12/02/25 13:51), Koichi Sasada wrote:

I think there are no reason why comma should be rejected.

It's simple.

diff --git a/parse.y b/parse.y
index e47dac4..f55e754 100644
--- a/parse.y
+++ b/parse.y
@@ -979,40 +979,40 @@ stmtorbegin : stmt
%*/
}

-stmt : keywordalias fitem {lexstate = EXPRFNAME;} fitem
+stmt : keyword
alias fitem optcomma {lexstate = EXPRFNAME;} fitem
{
/%%%/
- $$ = NEW
ALIAS($2, $4);
+ $$ = NEWALIAS($2, $5);
/%
- $$ = dispatch2(alias, $2, $4);
+ $$ = dispatch2(alias, $2, $5);
%
/
}
- | keyword
alias tGVAR tGVAR
+ | keywordalias tGVAR optcomma tGVAR
{
/%%%/
- $$ = NEWVALIAS($2, $3);
+ $$ = NEW
VALIAS($2, $4);
/%
- $$ = dispatch2(varalias, $2, $3);
+ $$ = dispatch2(var
alias, $2, $4);
%
/
}
- | keywordalias tGVAR tBACKREF
+ | keywordalias tGVAR optcomma tBACKREF
{
/%%%/
char buf[2];
buf[0] = '$';
- buf[1] = (char)$3->nd
nth;
+ buf[1] = (char)$4->ndnth;
$$ = NEW
VALIAS($2, rbintern2(buf, 2));
/*%
- $$ = dispatch2(var
alias, $2, $3);
+ $$ = dispatch2(varalias, $2, $4);
%*/
}
- | keyword
alias tGVAR tNTHREF
+ | keyword
alias tGVAR optcomma tNTHREF
{
/%%%/
yyerror("can't make alias for the number variables");
$$ = NEWBEGIN(0);
/*%
- $$ = dispatch2(var
alias, $2, $3);
+ $$ = dispatch2(varalias, $2, $4);
$$ = dispatch1(alias
error, $$);
%/
}
@@ -5432,6 +5432,10 @@ opt_nl : /
none */
| '\n'
;

+optcomma : /* none */
+ | ','
+ ;
+
rparen : opt
nl ')'
;

--
Nobu Nakada

#6 Updated by Marc-Andre Lafortune about 2 years ago

I might have sounded more negative than I intended, so let me say that I also agree that we should allow a comma.

#7 Updated by Robert A. Heiler about 2 years ago

I myself prefer "alias" because:

  • It does not require a ','
  • It is shorter to type
  • It reads easier.

    def bar
    puts 'Hi from bar.'
    end

    alias foo bar
    alias_method :foo, :bar

The first way is more readable for my poor old eyes.

#8 Updated by Yusuke Endoh about 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Nobuyoshi Nakada

Sorry, I didn't know alias syntax accepts symbols as arguments!
Then I agree with this proposal.

Ruby always gives me a fresh surprise for me.

Yusuke Endoh mame@tsg.ne.jp

#9 Updated by Yusuke Endoh over 1 year ago

  • Target version changed from 2.0.0 to next minor

#10 Updated by Thomas Sawyer over 1 year ago

Is there any reason this can't make it into 2.0?

#11 Updated by Alexey Muranov over 1 year ago

=begin
Just another idea in this direction: allow

def :foo, :bar do
puts bar
end

in addition to

def foo(bar)
puts bar
end

(the "(({do}))" in the first case can be optional).
=end

#12 Updated by Eric Hodel over 1 year ago

On Dec 7, 2012, at 9:10, "alexeymuranov (Alexey Muranov)" redmine@ruby-lang.org wrote:

Issue #6074 has been updated by alexeymuranov (Alexey Muranov).

=begin
Just another idea in this direction:

If you have a separate idea open a separate feature request.

Do not hijack existing issues.

Hijacking issues makes them difficult for the committers to understand. Since this feature is delayed until next minor, continued off-topic discussion may lead to an issue being closed because its purpose is too difficult to determine.

Also available in: Atom PDF