Skip to content

Literal::Data is not behaving in a hash-like way #325

@rachelgraves

Description

@rachelgraves

According to the docs Literal::Data should behave in a hash-like way, however when I do the following it tells me [] is an undefined method.

Both are hash-like, meaning they can be used in place of a hash in some cases. You can access their properties using the [] method. Literal::Struct also supports []= to set properties.

Running v1.8

Sample code:

# frozen_string_literal: true

class ChartPoint < Literal::Data
  prop :x, String
  prop :y, _Union(Integer, Float)
end

Testing:

[1] pry(main)> f = ChartPoint.new(x: "foo", y: 12.1)
=> #<ChartPoint:0x00000001251bef10 @x="foo", @y=12.1>
[2] pry(main)> f[:x]
NoMethodError: undefined method `[]' for an instance of ChartPoint

Expected outcome:

[1] pry(main)> f = ChartPoint.new(x: "foo", y: 12.1)
=> #<ChartPoint:0x00000001251bef10 @x="foo", @y=12.1>
[2] pry(main)> f[:x]
=> "foo"

Literal::Struct

Works as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions