Project

General

Profile

Actions

Bug #9593

closed

Keyword arguments default argument assignment behaviour not consistent with optional argument

Added by chendo (Jack Chen) about 10 years ago. Updated almost 10 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)

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
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0