Project

General

Profile

Bug #9593

Keyword arguments default argument assignment behaviour not consistent with optional argument

Added by chendo (Jack Chen) about 6 years ago. Updated almost 6 years ago.

Status:
Closed
Priority:
Normal
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

Related to Ruby master - Bug #10314: Default argument lookup fails in Ruby 2.2 for circular shadowed variable namesClosednobu (Nobuyoshi Nakada)Actions
Related to Ruby master - Bug #11074: Block with optional parameter of same closure variable nameRejected04/18/2015Actions
Has duplicate Ruby master - Bug #10280: Regression while evaluating default argments of a methodRejected09/22/2014Actions

Also available in: Atom PDF