Project

General

Profile

Actions

Bug #9593

closed

Keyword arguments default argument assignment behaviour not consistent with optional argument

Bug #9593: Keyword arguments default argument assignment behaviour not consistent with optional argument

Added by chendo (Jack Chen) over 11 years ago. Updated over 11 years ago.

Status:
Closed
Target version:
[ruby-core:61299]

Description

Given the following code:

def var 100 end def foo(var: var + 1) puts "var: #{var.inspect}" end def bar(var = var + 1) puts "var: #{var.inspect}" end foo(var: 1) foo rescue p $! bar(1) bar 

Ruby 2.0.0:

var: 1 var: 101 var: 1 var: 101 

Ruby 2.1.1:

var: 1 #<NoMethodError: undefined method `+' for nil:NilClass> var: 1 var: 101 

What appears to be happening is that since 2.1.1, the keyword argument defines var as a variable before evaluating the default argument. Personally, I prefer 2.0.0 behaviour, but the way 2.1.1 handles default arguments in non keyword arguments is inconsistent.


Related issues 3 (0 open3 closed)

Actions

Also available in: PDF Atom