1. 25
  1.  

  2. 4

    Article say 3.1 but this is a 3.2 feature.

    1. 3

      Indeed. Seems to be absent from https://ruby-doc.org/3.1.2/, but present in 3.2 ( https://ruby-doc.org/3.2.0.preview3/Data.html).

    2. 4

      Fuck yes, finally.

      1. 2

        Couldn’t you just use freeze on a Struct? Or is this just a more ergonomic way of doing this

        1. 3

          I think part of this is that it has a default #deconstruct, and as such can get used with pattern matching, which I don’t think can get added to Struct in a backwards compatible way (? I think?).

          So, this can work (from docs here: https://ruby-doc.org/3.2.0.preview3/Data.html#method-i-deconstruct)

          Measure = Data.define(:amount, :unit)
          
          distance = Measure[10, 'km']
          distance.deconstruct #=> [10, "km"]
          
          # later
          case distance
          in Measure(n, 'km')
            puts "It is #{n} kilometers away"
          # ...
          end
          
          1. 3

            Yeah ergonomics. You’d have to remember to freeze every new struct, or else make a wrapper that would essentially be this.