Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kt_data_class

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kt_data_class

  • 0.1.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

KtDataClass

Kotlinの

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

val p1 = Point(3, 4)

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

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>

FAQs

Package last updated on 01 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc