Class: Scheming::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/scheming/generic.rb

Overview

Generic

Many types can be “templated” in a way where some types get swapped in and out. Perhaps the most common example for this is with a two dimensional Point structure. We could have a point with Float coordinates OR they could also be Integer:

“‘ruby Object(x: Float, y: Float) Object(x: Integer, y: Integer) “`

Instead of having two defintions we can have a single generic that defins both:

“‘ruby Point = Scheming::Generic.new do |(t)|

Object(x: t, y: t)

end

Point

Point “‘

Instance Method Summary collapse

Constructor Details

#initialize(&proto_type) ⇒ Generic

Returns a new instance of Generic.



31
32
33
# File 'lib/scheming/generic.rb', line 31

def initialize(&proto_type)
  @proto_type = proto_type
end

Instance Method Details

#[](*types) ⇒ Object

Parameters:



36
37
38
39
40
# File 'lib/scheming/generic.rb', line 36

def [](*types)
  Scheming::DSL::DataBuilder
    .new
    .instance_exec(types, &@proto_type)
end