include? needs parentheses?

G

Gavin Sinclair

I find this kind of odd.

$ ruby -w
puts([1,2,3].include? 1)
-:1: warning: parenthesize argument(s) for future version
true

Surely there's no ambiguity in that expression. What principle is at work
here?

Sorry if this is similar to other cases already discussed. I was paying
attention, but those cases seemed more ambiguous, hence the warning.

Gavin
 
R

Robert Klemme

Gavin Sinclair said:
I find this kind of odd.

$ ruby -w
puts([1,2,3].include? 1)
-:1: warning: parenthesize argument(s) for future version
true

Surely there's no ambiguity in that expression. What principle is at work
here?

Sorry if this is similar to other cases already discussed. I was paying
attention, but those cases seemed more ambiguous, hence the warning.

I can only guess: maybe it's because the line "puts([1,2,3].include? 1,2)"
would be amiguous?

robert
 
N

nobu.nokada

Hi,

At Wed, 14 Apr 2004 18:39:23 +0900,
Robert Klemme wrote in [ruby-talk:97118]:
Sorry if this is similar to other cases already discussed. I was paying
attention, but those cases seemed more ambiguous, hence the warning.

I can only guess: maybe it's because the line "puts([1,2,3].include? 1,2)"
would be amiguous?

You're correct, and Gavin may be also.


Index: parse.y
===================================================================
RCS file: /cvs/ruby/src/ruby/parse.y,v
retrieving revision 1.319
diff -U2 -p -d -r1.319 parse.y
--- parse.y 5 Apr 2004 13:16:40 -0000 1.319
+++ parse.y 14 Apr 2004 11:17:50 -0000
@@ -119,4 +119,5 @@ static void void_expr0();
static void void_stmts();
static NODE *remove_begin();
+static void warn_to_parenthesize();
#define value_expr(node) value_expr0((node) = remove_begin(node))
#define void_expr(node) void_expr0((node) = remove_begin(node))
@@ -1217,5 +1218,5 @@ aref_args : none
| command opt_nl
{
- rb_warn("parenthesize argument(s) for future version");
+ warn_to_parenthesize($1);
$$ = NEW_LIST($1);
}
@@ -1250,5 +1251,5 @@ paren_args : '(' none ')'
| '(' block_call rparen
{
- rb_warn("parenthesize argument for future version");
+ warn_to_parenthesize($2);
$$ = NEW_LIST($2);
}
@@ -1266,5 +1267,5 @@ opt_paren_args : none
call_args : command
{
- rb_warn("parenthesize argument(s) for future version");
+ warn_to_parenthesize($1);
$$ = NEW_LIST($1);
}
@@ -4512,4 +4513,17 @@ fixpos(node, orig)
node->nd_file = orig->nd_file;
nd_set_line(node, nd_line(orig));
+}
+
+static void
+warn_to_parenthesize(node)
+ NODE *node;
+{
+ switch (nd_type(node)) {
+ case NODE_CALL:
+ case NODE_FCALL:
+ if (!node->nd_args) return;
+ if (node->nd_args->nd_alen == 1) return;
+ }
+ rb_warn("parenthesize argument(s) for future version");
}
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: include? needs parentheses?"

|> I can only guess: maybe it's because the line "puts([1,2,3].include? 1,2)"
|> would be amiguous?
|
|You're correct, and Gavin may be also.

The basic rule was "when you use return values from method calls, use
parentheses". So I believe I didn't make mistake for this case. But
your expectation might be worth consideration.

matz.
 
N

nobu.nokada

Hi,

At Thu, 15 Apr 2004 02:34:49 +0900,
Yukihiro Matsumoto wrote in [ruby-talk:97164]:
|> I can only guess: maybe it's because the line "puts([1,2,3].include? 1,2)"
|> would be amiguous?
|
|You're correct, and Gavin may be also.

The basic rule was "when you use return values from method calls, use
parentheses". So I believe I didn't make mistake for this case. But
your expectation might be worth consideration.

Understood. Then, the messages shouldn't be so?
 
E

Emmanuel Touzery

Hello,

Hi,

At Thu, 15 Apr 2004 02:34:49 +0900,
Yukihiro Matsumoto wrote in [ruby-talk:97164]:

|> I can only guess: maybe it's because the line "puts([1,2,3].include? 1,2)"
|> would be amiguous?
|
|You're correct, and Gavin may be also.

The basic rule was "when you use return values from method calls, use
parentheses". So I believe I didn't make mistake for this case. But
your expectation might be worth consideration.

Understood. Then, the messages shouldn't be so?
yes, that'd help the messages to be much less "try and fix" if people
understand them less intuitively and more objectively.

emmanuel
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: include? needs parentheses?"

|> The basic rule was "when you use return values from method calls, use
|> parentheses". So I believe I didn't make mistake for this case. But
|> your expectation might be worth consideration.
|
|Understood. Then, the messages shouldn't be so?

What would be a suitable message? Or is omitting parentheses better
than tweaking message?

matz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top