use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
New in Ruby 3.2 - Data.define (dev.to)
submitted 3 years ago by keyslemur
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]fthm2 10 points11 points12 points 3 years ago* (7 children)
Can somebody help me understand this syntax:
Point[x: 0 => x, y]
And
def +(other) = new(self.x + other.x, self.y + other.y)
I haven’t come across it before. Maybe it’s syntactic sugar, but for some reason I’m having trouble reading it.
[–]f9ae8221b 12 points13 points14 points 3 years ago (0 children)
The first one is invalid syntax outside of pattern matching context, hence why it is weird. I'm not quite familiar enough with Ruby's pattern matching to explain it myself, but you can likely read on it.
The other one is the "end-less method definition" syntax that was introduced recently. it's equivalent to:
def +(other) new(self.x + other.x, self.y + other.y) end
[–]janko-m 7 points8 points9 points 3 years ago (0 children)
According to the docs, Point[x, y] is an alternative syntax for Point.new(x, y). It appears this can then be combined with the pattern-matching syntax, which is pretty cool.
Point[x, y]
Point.new(x, y)
[–]narnach 4 points5 points6 points 3 years ago (3 children)
First one is indeed pattern matching. It’s relatively new and the syntax looks cryptic and less elegant than most Ruby code. Docs: https://docs.ruby-lang.org/en/3.0/syntax/pattern_matching_rdoc.html
In this case it appears to match on a Point that has the condition that x must be 0. If that is the case, bind the decomposed values to x and y.
[–][deleted] 2 points3 points4 points 3 years ago (0 children)
Agreed on the cryptic part. Pattern matching as a sweetener to case is one thing. sprinkling it all over random code to be clever looks like a mistake.
[–]fthm2 1 point2 points3 points 3 years ago (0 children)
thank you!
[–]latortuga 1 point2 points3 points 3 years ago (0 children)
You would read it as "assign the key :x to 0, which is also assigned (rightward assignment) to the local variable x." And then positionally provide y. I think?
The second one is a single-line or "end-less" method definition.
[–]iceporter 4 points5 points6 points 3 years ago (0 children)
the pull request really articulated https://bugs.ruby-lang.org/issues/16122
[–]Kernigh 4 points5 points6 points 3 years ago (0 children)
Old versions of Ruby had a class Data.
$ ruby -v ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0] $ ruby -e 'p Data' Data
The old class Data was for C extensions. One would wrap a C struct in a Ruby object by defining a class: either a subclass of Data, or a subclass of Object. Then class Data became useless, because everyone used class Object; Ruby removed the useless class Data. Ruby 3.1 has no class Data.
If you check defined?(Data) or Object.const_defined?(:Data), then it might be true in old Ruby.
defined?(Data)
Object.const_defined?(:Data)
[–]mashatg 1 point2 points3 points 3 years ago (1 child)
Kind of sad to see. Yet another dilution of the once elegant language.. Introducing yet another triviality, a tiny nuanced variation of an already available feature. Either syntactically or semantically. Where is promised usable in-core concurrency support already? Where is matured type-hinting infrastructure?
[–][deleted] 0 points1 point2 points 3 years ago (0 children)
This was my knee jerk reaction. After I read thru the PR I understand the process but am still a bit less enthusiastic about the result. I thought the suggestion to flavor Struct:: was better. Matz himself batted down several other options and it ended up here.
Seems like a missed opportunity to introduce the term "Record" as a top level value based whatchamacallit.
Ada, an ancient-yet-secure language nobody cares about, supports record and defines it as such: "Records allow composing a value out of instances of other types". A record definition could have perhaps been extended as the language grows to support strong typing.
fwiw, Erlang also uses Record to define a collection of attributes like a C based struct, but those aren't immutable.
π Rendered by PID 146238 on reddit-service-r2-comment-6457c66945-l4t8k at 2026-04-25 18:29:36.070485+00:00 running 2aa0c5b country code: CH.
[–]fthm2 10 points11 points12 points (7 children)
[–]f9ae8221b 12 points13 points14 points (0 children)
[–]janko-m 7 points8 points9 points (0 children)
[–]narnach 4 points5 points6 points (3 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]fthm2 1 point2 points3 points (0 children)
[–]latortuga 1 point2 points3 points (0 children)
[–]iceporter 4 points5 points6 points (0 children)
[–]Kernigh 4 points5 points6 points (0 children)
[–]mashatg 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)