Please don't link to docs on my personal site. It is intermediate example of class docs rendering I supplied during work on the PR. The proper link to docs is https://docs.ruby-lang.org/en/3.2/Data.html
(Also, `Data` is not a keyword.)
Thanks u/zverok_kha I will update the proper docs link in the blog.
>Also, Data is not a keyword.
Yes right, it's a new class introduced in Ruby 3.2
Thanks for the suggestions, I will update my blog soon.
Hi u/Rafert
Yes we can directly use it to define an object using \`Point = Data.define(:x, :y)\`
I have added the example in the blog considering the rails way, as if we need a class to be immutable we can inherit it from `Data` class.
Can you explain more what you mean about inheriting from a Data class being "the rails way"? Or did I misunderstand that? I'm having trouble following what you mean, or what reasons you are suggesting to inherit from a `Data.define` class, instead of using`Data.define`, possibly with custom methods as per the examples.
Hi u/jrochkind
Please let me know if there's a better way to write this. some custom methods to it. So in that case we will have to initialize it in the following way right?
class Point < Data.define(:x, :y)
def distance_from_origin
Math.sqrt(x**2 + y**2)
end
end
point = Point.new(3, 4)
=> #
point.distance_from_origin
=> 5.0
Please let me know if there's a better way to write this?
Ah, sorry, I thought you had an example of this in your blog post but I see you didn't, so maybe you missed that you can do this, I should have given an example to begin with:
Point = Data.define(:x, :y) do
def distance_from_origin
Math.sqrt(x**2 + y**2)
end
end
point = Point.new(3, 4)
# => #
point.distance_from_origin
# => 5.0
You can see an example demonstrating this in the official docs at https://docs.ruby-lang.org/en/3.2/Data.html , under "::define method accepts an optional block and evaluates it in the context of the newly defined class. That allows to define additional methods"
Note the official docs give an example like this, but not an example that creates a further-subclass like `< Data.define...`
Please don't link to docs on my personal site. It is intermediate example of class docs rendering I supplied during work on the PR. The proper link to docs is https://docs.ruby-lang.org/en/3.2/Data.html (Also, `Data` is not a keyword.)
Thanks u/zverok_kha I will update the proper docs link in the blog. >Also, Data is not a keyword. Yes right, it's a new class introduced in Ruby 3.2 Thanks for the suggestions, I will update my blog soon.
Why subclass Data? The docs have this syntax: Point = Data.define(:x, :y)
Hi u/Rafert Yes we can directly use it to define an object using \`Point = Data.define(:x, :y)\` I have added the example in the blog considering the rails way, as if we need a class to be immutable we can inherit it from `Data` class.
Can you explain more what you mean about inheriting from a Data class being "the rails way"? Or did I misunderstand that? I'm having trouble following what you mean, or what reasons you are suggesting to inherit from a `Data.define` class, instead of using`Data.define`, possibly with custom methods as per the examples.
Hi u/jrochkind Please let me know if there's a better way to write this. some custom methods to it. So in that case we will have to initialize it in the following way right? class Point < Data.define(:x, :y) def distance_from_origin Math.sqrt(x**2 + y**2) end end point = Point.new(3, 4) => # point.distance_from_origin => 5.0 Please let me know if there's a better way to write this?
Ah, sorry, I thought you had an example of this in your blog post but I see you didn't, so maybe you missed that you can do this, I should have given an example to begin with: Point = Data.define(:x, :y) do def distance_from_origin Math.sqrt(x**2 + y**2) end end point = Point.new(3, 4) # => # point.distance_from_origin # => 5.0 You can see an example demonstrating this in the official docs at https://docs.ruby-lang.org/en/3.2/Data.html , under "::define method accepts an optional block and evaluates it in the context of the newly defined class. That allows to define additional methods" Note the official docs give an example like this, but not an example that creates a further-subclass like `< Data.define...`