[#102393] [Ruby master Feature#17608] Compact and sum in one step — sawadatsuyoshi@...

Issue #17608 has been reported by sawa (Tsuyoshi Sawada).

13 messages 2021/02/04

[#102438] [Ruby master Bug#17619] if false foo=42; end creates a foo local variable set to nil — pkmuldoon@...

Issue #17619 has been reported by pkmuldoon (Phil Muldoon).

10 messages 2021/02/10

[#102631] [Ruby master Feature#17660] Expose information about which basic methods have been redefined — tenderlove@...

Issue #17660 has been reported by tenderlovemaking (Aaron Patterson).

9 messages 2021/02/27

[#102639] [Ruby master Misc#17662] The herdoc pattern used in tests does not syntax highlight correctly in many editors — eregontp@...

Issue #17662 has been reported by Eregon (Benoit Daloze).

13 messages 2021/02/27

[#102652] [Ruby master Bug#17664] Behavior of sockets changed in Ruby 3.0 to non-blocking — ciconia@...

Issue #17664 has been reported by ciconia (Sharon Rosner).

23 messages 2021/02/28

[ruby-core:102545] [Ruby master Feature#17627] Suggestion: Implement `freeze_values` instance method on collection-like classes.

From: marcandre-ruby-core@...
Date: 2021-02-16 19:58:24 UTC
List: ruby-core #102545
Issue #17627 has been updated by marcandre (Marc-Andre Lafortune).


Could you provide some actual real-world use cases?

----------------------------------------
Feature #17627: Suggestion: Implement `freeze_values` instance method on collection-like classes.
https://bugs.ruby-lang.org/issues/17627#change-90446

* Author: keithrbennett (Keith Bennett)
* Status: Open
* Priority: Normal
----------------------------------------
Suggestion: Implement `freeze_values` instance method on collection-like classes.

By collection-like classes, I mean classes such as Array, Hash, Set, Struct, OpenStruct, and custom classes containing multiple objects.

There has been some discussion of a recursive `deep_freeze` method, and although it could be very useful, there are potential problems regarding unintended consequences, and guarding against these would make the implementation more complex.

This complexity could be greatly reduced if we limit the scope of the freeze to only one level, and have the new `freeze_values` method call `freeze`.

The implementation would be trivial, I think. For example:

```
class Array  # same for Set
  def freeze_values
    each(&:freeze)
  end
end

class Hash
  def freeze_values
    values.each(&:freeze)
  end
end
```

There would still be a risk that the programmer would call this when some values should not be frozen, but that risk would be smaller and more manageable.

Also, there are many cases in which these collections contain simple objects such as strings and numbers. In these cases, recursion would not be necessary or helpful.

Although it could be argued that the implementation is so trivial that it does not need a method implemented, I believe that:

1) the method would nevertheless simplify the task, encouraging freezing
2) the method name would be a higher level description of the operation, making the code more readable
3) custom classes could implement this contract in their own way, yielding the benefits of polymorphism

What do you think?




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