Project

General

Profile

Actions

Bug #12073

closed

local variable interferes with keyword arguments

Added by josh.cheek (Josh Cheek) almost 9 years ago. Updated over 8 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin13]
[ruby-core:73816]

Description

This code causes a syntax error:

def a(b:)
  b
end
a = 1
a b: 9
 
# !> program.rb:5: syntax error, unexpected ':', expecting end-of-input
# !> a b: (9)
# !>     ^

However, if we remove the local assignment it works

def a(b:)
  b
end
a b: 9 # => 9

Or if we use parentheses it works

def a(b:)
  b
end
a = 1
a(b: 9) # => 9

Or if we use non-keyword arguments it works

def a(b)
  b
end
a = 1
a 9 # => 9

Updated by seanlinsley (Sean Linsley) almost 9 years ago

This is also an issue with array destructuring:

def a(*b)
  b
end

a = [1,2,3]

a a
 => [[1, 2, 3]] 

a *a
TypeError: no implicit conversion of Array into Integer

a(*a)
 => [1, 2, 3]

This is on Ruby 2.2.3.

Updated by nobu (Nobuyoshi Nakada) almost 9 years ago

  • Description updated (diff)
  • Status changed from Open to Closed
  • Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED

Sean Linsley wrote:

This is also an issue with array destructuring:

You are warned.

$ ruby -w -e 'def a(*b); b; end' -e 'a=[1,2,3]' -e 'a *a'
-e:3: warning: `*' after local variable or literal is interpreted as binary operator
-e:3: warning: even though it seems like argument prefix
-e:3: warning: possibly useless use of * in void context
-e:3:in `*': no implicit conversion of Array into Integer (TypeError)
	from -e:3:in `<main>'

Updated by nagachika (Tomoyuki Chikanaga) over 8 years ago

  • Backport changed from 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED to 2.1: REQUIRED, 2.2: REQUIRED, 2.3: DONE

ruby_2_3 r54618 merged revision(s) 53834,53835,53836.

Updated by usa (Usaku NAKAMURA) over 8 years ago

  • Backport changed from 2.1: REQUIRED, 2.2: REQUIRED, 2.3: DONE to 2.1: REQUIRED, 2.2: DONE, 2.3: DONE

ruby_2_2 r54676 merged revision(s) 53834,53835,53836.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0