4 ms·
I love Ruby, but I honestly don't get the point of Struct and now Data. You can do the same thing as both Struct and Data with Class. An immutable data containe
by corytheboyd 4y ago
I love Ruby, but I honestly don't get the point of Struct and now Data. You can do the same thing as both Struct and Data with Class. An immutable data container is a Class without setter functions. Whenever you start with a Struct that now needs methods to operate on private data, you just have a Class.
Whatever, I still love you Ruby. You can always just not use these things and life goes on.
- chc 4y agoData is shorter to write and gives you automatic structural equality. You really could do the same thing with a class, but if you want something exactly like Data, it's nice to have it built in.
- berkes 4y agoI'm a big fan of value objects. In Ruby, many of my projects have a simple baseclass, ValueObject, that is only a little simpler than Data. I love it that I can now use it from the language instead. It saves me just one class. At most five methods. But it saves me having to explain and defend this for colleagues who insist that an email is Just A String, or a price being a float (i know) has worked Just Fine. I can now point them at stdclass documentation. And it being part of std weighs far more than the opinion of that contractor that keeps on nagging about design patterns and crap.
- bfung 4y agoThey’re both shortcuts to creating class-like things. Take @dataclass in python for example, that’s similar to Struct for Ruby. ‘Data’ documentation from the GitHub PR (https://zverok.space/ruby-rdoc/Data.html https://zverok.space/ruby-rdoc/Data.html): Data provides no member writers, or enumerators: it is meant to be a storage for immutable atomic values. But note that if some of data members is of a mutable class, Data does no additional immutability enforcement See also Struct, which is a similar concept, but has more container-alike API, allowing to change contents of the object and enumerate it.
- masklinn 4y ago> Take @dataclass in python for example, that’s similar to Struct for Ruby. From a cursory look data classes seem to do a lot more than structs. Structs seem like a mutable version of namedtuples, or js objects, down to behaving like a collection / iterable.
- epgui 4y agoVery often, in engineering or design or even art, the best feature is actually a constraint.
- airstrike 4y ago"Readability counts."
- pdntspa 4y agoHonestly they could have made Struct simply be a hash with some syntactic sugar, I can't say I've ever used Struct in anything I've written in my 8+ years writing Ruby professionally With this new data class... Idunno I'm shrugging my shoulders. The whole mutability/immutability debate has always seemed so pointless. Don't go assigning shit so willy-nilly?
- deleted 4y ago[deleted]
- kayodelycaon 4y agoI use Struct for some things because it enforces and documents the shape of the data. This is especially helpful when code gets complicated and you have a lot of hashes being thrown around. Instead of trying to document and enforce a nested hash structure through multiple layers, you can use Structs to make mini classes that do that for you.
- weaksauce 4y agofrom a reddit comment: "Glad we'll have a built-in struct class that's not Enumerable. I remember passing a Struct instance to a method which had a check for object.is_a?(Enumerable), and then proceeded to do something else than what I intended. It's fine to offer a way to iterate over attributes, but it was unnecessary to make the whole Struct Enumerable."
- Lammy 4y agoThis is the use-case for me. Here's an actual example of a Struct I will probably convert to Data in the file-identification library I've been working on. Right now they just have their `#to_a` overridden to disable some of their annoying automatic Enumerable behavior: https://github.com/okeeblow/DistorteD/blob/dd2a99285072982d32e69a2e63d204b0d4aff262/CHECKING%20YOU%20OUT/lib/checking-you-out/inner_spirit.rb#L82-L91 https://github.com/okeeblow/DistorteD/blob/dd2a99285072982d3...
- bitwize 4y agoRuby gonna Ruby. I decided it wasn't for me when I realized that blocks had magic syntax and were separate from, but convertible to, anonymous procedures. But some people love it, and if it's to your taste then it's to your taste.
- bigtunacan 4y agoIn reality there is a lot of overlap between structs and classes that the intent seems to be a bit lost on modern devs. They were intended (going back at least to C++) to convey different things. A struct is a light abstraction of related things. So instead of having int x, int y everywhere you have Struct Point. A class has responsibilities, implies state, implies interfaces. Given the signaling that structs send they should be value objects and as such the original Ruby struct is a bad implementation. The Data object is basically a concession of this with an understanding that changing struct would break "all the things" that already use struct.
- vidarh 4y agoIn C++ a struct from the start was a class with default public visibility. You can define member function on them.
- bigtunacan 4y agoYes I'm aware. You can define methods/functions on most (if not all implementations of struct); Ruby's is no exception to this. The parent indicated they didn't understand why both struct/class exist because there is so much overlap. The primary reason is to signal intent. This has largely been lost as the masses just use classes for everything or they use simple data types like hashes.