[#71439] [Ruby trunk - Feature #11339] [PATCH] io.c: avoid kwarg parsing in C API — matz@...
Issue #11339 has been updated by Yukihiro Matsumoto.
7 messages
2015/11/11
[#71473] Re: [Ruby trunk - Feature #11339] [PATCH] io.c: avoid kwarg parsing in C API
— Eric Wong <normalperson@...>
2015/11/13
Entire series for sockets
[#71450] Ruby 2.3.0-preview1 Released — "NARUSE, Yui" <naruse@...>
Hi,
5 messages
2015/11/11
[#71617] [Ruby trunk - Feature #11664] [PATCH] introduce rb_autoload_value to replace rb_autoload — nobu@...
Issue #11664 has been updated by Nobuyoshi Nakada.
3 messages
2015/11/20
[#71721] [Ruby trunk - Feature #11741] Migrate Ruby to Git from Subversion — me@...
Issue #11741 has been updated by Jon Moss.
4 messages
2015/11/28
[ruby-core:71553] [Ruby trunk - Feature #11708] [Open] Specify a way to override Struct-subclass constructor
From:
prijutme4ty@...
Date:
2015-11-18 14:02:16 UTC
List:
ruby-core #71553
Issue #11708 has been reported by Ilya Vorontsov.
----------------------------------------
Feature #11708: Specify a way to override Struct-subclass constructor
https://bugs.ruby-lang.org/issues/11708
* Author: Ilya Vorontsov
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
It's common to create simple data-object with some constraints. One can either implement custom class or use `Struct`. Struct is generally simpler and helps to avoid some mistakes as non-defined `#hash` and `#eql?`. But at the same time it's more difficult to make validation for `Struct` subclass.
```ruby
Point = Struct.new(:x, :y)
NonnegativePoint = Struct.new(:x,:y) do
def initialize(*args, &block)
super
raise 'Negative coordinates are not allowed' if x < 0 || y < 0
end
end
```
Above written code solves the problem but has one flaw. `Struct.new` creates a subclass of `Struct` and defines some methods as `#x`, `#x=`. And there are no guarantees that `NonnegativePoint#initialize` wasn't redefined too.
We can check that `Point.new` without explicitly defined `#initialize` actually hits `Struct#initialize` and `Point#initialize` not defined:
```ruby
Point.instance_method(:initialize)
# => #<UnboundMethod: Point(Struct)#initialize>
NonnegativePoint.instance_method(:initialize)
# => #<UnboundMethod: NonnegativePoint#initialize>
```
But nothing in `Struct` documentation or test suite states that this behavior can't be changed in newer ruby versions.
I propose either to declare in docs and test that initialize method can be safely overriden because `#initialize` is not defined in `Struct` subclasses.
In you assume that one day current behavior can change (e.g. for perfomance reasons), then it's reasonable to create an extension point like '#after_initialize' which is called from `Struct`'s subclass `#initialize` method.
--
https://bugs.ruby-lang.org/