Issue #12861 has been updated by Jeremy Evans.


bug hit wrote:
> Jeremy Evans wrote:
> > method calls in blocks are sometimes lexical and sometimes dynamic, depending on how the block is invoked.
> > 
> Method resolution is never lexical, it is always relative to the current, dynamic self.

If method resolution is always dynamic, and super is directly related to method resolution, it would certainly be odd for super to always be lexical, right?

One could argue that super is currently always relative to the current, dynamic method. In your example:

~~~ ruby
  def self.bar(&block)
    a = block.()       # super is called in method foo
    define_singleton_method :method1, &block
    b = send(:method1) # super is called in method method1
    c = block.()       # super is called in method foo
    [a, b, c]
  end
~~~

I think ruby's current behavior makes sense.  super should operate on the current method. 

If you really want Class2.method1 to call Class1.foo, use super_method in Class2.foo to get the appropriate Method object, and call that:

~~~ ruby
class Class2 < Class1
  def self.foo
    meth = method(__method__).super_method
    bar do
      meth.call
    end
  end
end
~~~

----------------------------------------
Bug #12861: super in a block can be either lexically or dynamically scoped depending on how the block is invoked
https://bugs.ruby-lang.org/issues/12861#change-61052

* Author: bug hit
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
```ruby
class Class1
  def self.foo
    'foo'
  end
  def self.method1
    'method1'
  end
end

class Class2 < Class1
  def self.foo
    bar do
      super()
    end
  end
  def self.bar(&block)
    a = block.()
    define_singleton_method :method1, &block
    b = send(:method1)
    c = block.()
    [a, b, c]
  end
end

p Class2.foo # ["foo", "method1", "foo"]
```

It doesn't seem like a good idea for a given language construct to be either lexically or dynamically scoped, depending on how its surrounding block is invoked (which is not visible at the point of definition).  I think it would be better if super were always lexically scoped, and a different keyword (dynamic_super) were always dynamically scoped



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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>