Forum: Ruby-core Warning for trailing comma in method declarations

Posted by Kornelius Kalnbach (Guest)
on 2008-09-09 23:14
(Received via mailing list)
hello!

Ruby allows code like this:

  def file_replace filename, file,
    files = document.files

In this case, it was a typo, and could have produced problems. I'm not
sure how many people are using multi-line method declarations
intentionally, but I think they are few.

So I suggest that the Ruby parser issues a warning, like:

  warning: trailing comma in method declaration

It should be possible to silence the warning:

  def file_replace filename, file, \
    files = document.files

with the backslash making clear what you want here.

What do you think?
[murphy]
Posted by Jeremy Kemper (Guest)
on 2008-09-09 23:22
(Received via mailing list)
On Tue, Sep 9, 2008 at 2:08 PM, Kornelius Kalnbach <murphy@rubychan.de> 
wrote:
>
>
> What do you think?
> [murphy]

Putting parentheses around your method arguments is a simpler, cleaner
solution, I think.

I'd hate to have to introduce \ to avoid a warning!

Best,
jeremy
Posted by David A. Black (Guest)
on 2008-09-09 23:23
(Received via mailing list)
Hi --

On Wed, 10 Sep 2008, Kornelius Kalnbach wrote:

>
>
> What do you think?

I believe the comma already makes it clear what you want. I would not
like to get a warning and have to put in a slash. I'd rather just live
with the general rule. I think that in every imaginable case, if you
didn't mean to put the comma, you'd find out very quickly and could
fix it.


David
Posted by Kornelius Kalnbach (Guest)
on 2008-09-09 23:38
(Received via mailing list)
David A. Black wrote:
> I think that in every imaginable case, if you
> didn't mean to put the comma, you'd find out very quickly and could
> fix it.
That's the problem: If the "first line" of your method is an assignment,
Ruby interprets it as an optional parameter, and everything works, as
long as you don't use the additional parameter. It could lead to some
really strange errors - or even security problems, maybe.

Jeremy: Problem is, I don't like parentheses, and never use them unless
I have to :) I consider them noise. But indeed, this case is an argument
for the using them.

Anyway,

  def file_replace(filename, file,
    files = document.files)

shouldn't create a warning either.

[murphy]
Posted by David A. Black (Guest)
on 2008-09-10 02:50
(Received via mailing list)
Hi --

On Wed, 10 Sep 2008, Kornelius Kalnbach wrote:

> David A. Black wrote:
>> I think that in every imaginable case, if you
>> didn't mean to put the comma, you'd find out very quickly and could
>> fix it.
> That's the problem: If the "first line" of your method is an assignment,
> Ruby interprets it as an optional parameter, and everything works, as
> long as you don't use the additional parameter. It could lead to some
> really strange errors - or even security problems, maybe.

But at some point, when you test it, you'll realize that something
isn't working right. I definitely would not want to be warned about
it, which is tantamount to making it illegal since putting a \ at the
end of the line is ugly and redundant.


David
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.