Project

General

Profile

Actions

Bug #10456

closed

kwargs and block without comma should probably be a syntax error

Added by bobbytables (Robert Ross) over 9 years ago. Updated over 9 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
[ruby-core:66007]

Description

ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]

I experienced something very weird, when you define a method like this:

def wat(something: nil &block)
end

It actually doesn't come back as a syntax error

Roberts-MacBook-Pro-2:~ robert$ cat test.rb; echo
def wat(name: "" &block)
end
Roberts-MacBook-Pro-2:~ robert$ ruby -c test.rb
Syntax OK

Within the method, the block is never assigned either actually on method call:

irb(main):007:0> def wat(something: nil &block)
irb(main):008:1> puts "Block: #{block}"
irb(main):009:1> end
=> :wat
irb(main):010:0> wat(something: 'hello') { "My block here" }
NameError: undefined local variable or method `block' for main:Object
  from (irb):8:in `wat'
  from (irb):10
  from /Users/robert/.rbenv/versions/2.1.1/bin/irb:11:in `<main>'

This behavior seems unlikely, it caught me off guard for sure.

Updated by Anonymous over 9 years ago

def wat(something: nil &block)
end

nil &block is a legal expression (calling the & method on nil with block as the argument).

The expression is evaluated at method call time, not method definition time, so that's where the (correct) error shows up.

Updated by Anonymous over 9 years ago

In fact, if you run with warnings enabled, you see:

$ ruby -w -e "def wat(something: nil &block); end"
-e:1: warning: `&' after local variable or literal is interpreted as binary operator
-e:1: warning: even though it seems like argument prefix

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

  • Description updated (diff)
  • Status changed from Open to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0