Issue #9593 has been updated by Nobuyoshi Nakada.


Aaron Patterson wrote:
> Was this expected to break?

Yes.  It was a bug.
An assignment creates a variable and it hides same name method in its RHS,
as you can't call `foo` method inside `bar` method:

~~~
  def foo; 'foo'; end

  def bar
    foo = foo # nil
  end
~~~

> Unfortunately we have real code that depends on the behavior:
> 
>   https://github.com/rails/rails/blob/81d08abcccf2ef1b0ea6e98daf00f6ca375f3d8a/activerecord/lib/active_record/associations/has_many_association.rb#L74

Sorry for the bug.

> but we can change it

Always you can call the method explicitly with `()`.


----------------------------------------
Bug #9593: Keyword arguments default argument assignment behaviour not consistent with optional argument
https://bugs.ruby-lang.org/issues/9593#change-45750

* Author: Jack Chen
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* Category: syntax
* Target version: current: 2.2.0
* ruby -v: 2.1.1
* Backport: 1.9.3: REQUIRED, 2.0.0: REQUIRED, 2.1: REQUIRED
----------------------------------------
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.



-- 
http://bugs.ruby-lang.org/