0.01
No commit activity in last 3 years
No release in over 3 years
All methods that alter the contents of an array that implements this Gem are first checked to ensure that the added items are of the types allowed. All methods behave exactly as their Array counterparts, including additional forms, block processing, etc. Defining a TypedArray Class: ```ruby class ThingsArray < Array extend TypedArray restrict_types Thing1, Thing2 end things = ThingsArray.new ``` Generating a single TypedArray ```ruby things = TypedArray(Thing1,Thing2).new These classes can be extended, and their accepted-types appended to after their initial definition.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.0
~> 1.6.4
>= 0
~> 2.3.0
 Project Readme

typed-array¶ ↑

Gem provides enforced-type functionality to Arrays

Copyright © 2011 Ryan Biesemeyer See LICENSE.txt for details

Ryan Biesemeyer ruby-dev@yaauie.com

Example¶ ↑

Create Standard Class¶ ↑

require 'typed-array'
class Things < Array
  extend TypedArray
  restrict_types Thing1,Thing2
end

Generate Class using Factory¶ ↑

require 'typed-array'
things = TypedArray(Thing1,Thing2)

Adding items to the Array¶ ↑

# All standard Array interfaces are implemented, including block-processing
# and variable-number of arguments. For methods that would usually return an
# Array, they instead return an instance of the current class (except to_a).
#
# The difference is that if the method would generate an Array including the
# wrong types, TypedArray::UnexpectedTypeException is raised and the call is
# aborted before modifying the enforced TypedArray instance.

require 'typed-array'
symbols = TypedArray(Symbol).new([:foo,:bar,:baz,:bingo])
begin
  integers = TypedArray(Integer).new([1,3,7,2,:symbol])
rescue TypedArray::UnexpectedTypeException
  puts "An error occured: #{$!}"
end