From: andy.am@...
Date: 2020-12-24T03:48:21+00:00
Subject: [ruby-core:101670] [Ruby master Bug#16383] TracePoint does not report calls to attribute reader methods

Issue #16383 has been updated by AndyMaleh (Andy Maleh).


Hi,

I compensated for lack of this feature by implementing **manually-configured** computed data-binding in [Glimmer DSL for Tk](https://github.com/AndyObtiva/glimmer-dsl-tk#hello-computed) (Ruby Desktop Development GUI Library), [Glimmer DSL for SWT](https://github.com/AndyObtiva/glimmer-dsl-swt#hello-computed) (JRuby Desktop Development GUI Library), and [Glimmer DSL for Opal](https://github.com/AndyObtiva/glimmer-dsl-opal#hello-computed) (Pure Ruby Web GUI and Auto-Webifier of Desktop Apps via Opal on Rails).

Example:

``` ruby
 label {
   text bind(@contact, :name, computed_by: [:first_name, :last_name])
 }
```

Alternatively, the `#first_name` and `#last_name` attributes could be implemented in the model as methods that notify observers of `#name` that it changed when called.

Still, I'd like to go a step further and implement **automatic** computed data-binding with simpler syntax:

``` ruby
 label {
   text bind(@contact, :name)
 }
```

That way, Glimmer could automatically figure out through calling the `Contact#name` method that it calls the `first_name` and `last_name` attributes, and then hook observers to them for change notifications to update the GUI.

I was wondering if there is any chance to have this missing feature (tracing attribute calls) supported in Ruby 3.0 via an extra `ruby` command option (e.g. --trace-func) as per Charles Nutter's suggestion to avoid slowing down general app performance when this feature is not needed. Perhaps even make it configurable (e.g. --trace-func=attr_accessor for attribute-tracing only, or a comma separated list like --trace-func=methods,attr_reader) to ensure further that the performance hit is limited when the feature is needed.

I realize this feature (automatic computed data-binding) is very new and has not been proven as feasible for practical application development, but I have not seen it implemented anywhere else (e.g. Java/JavaScript) with this level of automation (knowing which variables to observe without manual configuration), and I believe Ruby is the language to make that happen if any, so I'd really like to give it a go.

Cheers,

Andy Maleh

----------------------------------------
Bug #16383: TracePoint does not report calls to attribute reader methods
https://bugs.ruby-lang.org/issues/16383#change-89490

* Author: AndyMaleh (Andy Maleh)
* Status: Open
* Priority: Normal
* ruby -v: ruby 2.5.7p206 (2019-10-01 revision 67816) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
TracePoint does not report calls to attribute reader methods (e.g. methods defined using `attr_accessor` or `attr_reader`.)

**Code sample to demonstrate:**

```ruby
class Person
  attr_accessor :first_name
  attr_accessor :last_name
  def name
    "#{self.last_name}, #{self.first_name}"
  end
end

person = Person.new
person.first_name = 'Josh'
person.last_name = 'McGibbon'
trace = TracePoint.new(:call) do |tp|
  p [tp.path, tp.lineno, tp.defined_class, tp.event, tp.method_id]
end
trace.enable
person.name
trace.disable

class Person
  attr_writer :first_name
  attr_writer :last_name
  def name
    "#{self.last_name}, #{self.first_name}"
  end
  def first_name
    @first_name
  end
  def last_name
    @last_name
  end
end

person = Person.new
person.first_name = 'Josh'
person.last_name = 'McGibbon'
trace = TracePoint.new(:call) do |tp|
  p [tp.path, tp.lineno, tp.defined_class, tp.event, tp.method_id]
end
trace.enable
person.name
trace.disable
```

**Output:**

```
["trace_point_issue.rb", 4, Person, :call, :name]
["trace_point_issue.rb", 22, Person, :call, :name]
["trace_point_issue.rb", 28, Person, :call, :last_name]
["trace_point_issue.rb", 25, Person, :call, :first_name]
```

Please note how `:last_name` and `:first_name` show up only the second time `Person#name` is called. In other words, they show up when defined as actual methods using `def` keyword, but not when defined via `attr_accessor`.

**Expected Output:**

```
["trace_point_issue.rb", 22, Person, :call, :name]
["trace_point_issue.rb", 28, Person, :call, :last_name]
["trace_point_issue.rb", 25, Person, :call, :first_name]
["trace_point_issue.rb", 22, Person, :call, :name]
["trace_point_issue.rb", 28, Person, :call, :last_name]
["trace_point_issue.rb", 25, Person, :call, :first_name]
```

Your help in fixing or explaining this issue is greatly appreciated.

My goal is to monitor all method calls when invoking a certain method (Person#name in this case) in order to attach observers to them dynamically for desktop UI data-binding use in my open-source project Glimmer: https://github.com/AndyObtiva/glimmer

Cheers,

Andy Maleh

---Files--------------------------------
trace_point_issue.rb (791 Bytes)
tracepoint-attr-16383.patch (1.97 KB)


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