Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Allow `[]` to initialize Literal::Data (#336)

Proposal:

[Native Data
structure](https://docs.ruby-lang.org/en/master/Data.html#method-c-new)
allows objects to be instantied using `[]`, eg.
```ruby
Measure = Data.define(:amount, :unit)

Measure.new(amount: 1, unit: 'km')
#=> #<data Measure amount=1, unit="km">

Measure[amount: 1, unit: 'km']
#=> #<data Measure amount=1, unit="km">
```

I think Literal::Data should allow this syntax too to have an DX closer
to native Data structure. It was the first way I tried to instance a
Literal::Data and I got an error `undefined method '[]' for class Xyz`.

authored by

stephann and committed by
GitHub
e0b819bd 941af681

+9
+2
lib/literal/data.rb
··· 2 2 3 3 class Literal::Data < Literal::DataStructure 4 4 class << self 5 + def [](...) = new(...) 6 + 5 7 def prop(name, type, kind = :keyword, reader: :public, predicate: false, default: nil) 6 8 super(name, type, kind, reader:, writer: false, predicate:, default:) 7 9 end
+7
test/data.test.rb
··· 82 82 assert_equal(empty.eql?(other_empty), false) 83 83 assert_equal(empty.hash != other_empty.hash, true) 84 84 end 85 + 86 + test "initialize with [] method" do 87 + person_a = Person.new(name: "John") 88 + person_b = Person[name: "John"] 89 + 90 + assert_equal(person_a, person_b) 91 + end