[ruby-core:92468] [Ruby trunk Bug#15808] Forwardable doesn't allow forwarding the 'print' message

From: merch-redmine@...
Date: 2019-04-28 17:45:20 UTC
List: ruby-core #92468
Issue #15808 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Open to Rejected

This is not a bug in Forwardable, this is due to the fact that OpenStruct doesn't override `print`, as `Kernel#print` is already defined.  You must undefine the `print` method in OpenStruct if you want this to work.  Example:

```ruby
require 'forwardable'
require 'ostruct'

class A
  extend Forwardable
  def_delegators :@foo, :print, :bar

  def initialize(foo)
    @foo = foo
  end
end

os = OpenStruct.new(print: 123, bar: 456)
os.method(:print).owner
# => Kernel
os.singleton_class.send(:undef_method, :print)
os.method(:print).owner
=> #<Class:#<OpenStruct:0x00000038d756dc80>>
obj = A.new(os)

p "print - #{obj.print}; bar - #{obj.bar}" 
# "print - 123; bar - 456"
```



----------------------------------------
Bug #15808: Forwardable doesn't allow forwarding the 'print' message
https://bugs.ruby-lang.org/issues/15808#change-77816

* Author: kyrylo (Kyrylo Silin)
* Status: Rejected
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin17]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
The following program forwards two messages to `@foo`: message 'print' and message 'bar'.
When I forward the 'print' message, I see a warning and the return value of `print` is always `nil`
(despite the fact that I specified my own return value).
When I forward the 'bar' message, it works as expected (I see the return value of 'bar').

``` ruby
require 'forwardable'
require 'ostruct'

class A
  extend Forwardable
  def_delegators :@foo, :print, :bar

  def initialize(foo)
    @foo = foo
  end
end

obj = A.new(OpenStruct.new(print: 123, bar: 456))

p "print - #{obj.print}; bar - #{obj.bar}" 
```

```
forwardable_bug.rb:15: warning: A#print at /Users/kyrylo/.rubies/ruby-2.6.0/lib/ruby/2.6.0/forwardable.rb:158 forwarding to private method OpenStruct#print
"print - ; bar - 456"
```



---Files--------------------------------
forwardable_bug.rb (250 Bytes)


-- 
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>

In This Thread

Prev Next