0.0
No commit activity in last 3 years
No release in over 3 years
The Ruby porting of `data class` in Kotlin
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

KtDataClass

Kotlinの

data class Point(val x: Int, val y: Int)

val p1 = Point(3, 4)

のように書けるやつをRubyで作ってみようという試み。

Installation

Add this line to your application's Gemfile:

gem 'kt_data_class'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install kt_data_class

Feature

Basic usage

Point = KtDataClass.create(:x, :y)
p1 = Point.new(x: 3, y: 4)

puts p1.x, p1.y

Also, some convenient methods can be added like below:

Point = KtDataClass.create(:x, :y) do
  def -(other)
    self.class.new(x: x - other.x, y: y - other.y)
  end

  def norm
    Math.sqrt(x * x + y * y)
  end
end

(Point.new(x: 4, y: 5) - Point.new(x: 1, y: 1)).norm
# => 5.0

Immutable

Profile = KtDataClass.create(:hobby)
profile = Profile.new(hobby: "Ruby")

profile.hobby = "Python"
# NoMethodError: undefined method `hobby=' for #<Profile:0x0000000000ee2668 @hobby="Ruby">

new_profile = profile.copy(hobby: "Python")

profile.hobby
# => "Ruby"
new_profile.hobby
# => "Python"

Destructuring assignment

Point = KtDataClass.create(:x, :y)
p = Point.new(x: 1, y: 2)
x, y = p

x
# => 1

y
# => 2

FAQ

How can I define default value?

In Kotlin,

data class Point(x: Int = 0, y: Int = 0)

This can be implemented by overriding constructor like below:

Point = KtDataClass.create(:x, :y) do
  def initialize(x: 0, y: 0)
    super
  end
end

Point.new
# => #<Point:0x00000000020c5b78 @x=0, @y=0>

Point.new(x: 1)
# => #<Point:0x00000000020a90e0 @x=1, @y=0>