Class: Array

Inherits:
Object show all
Defined in:
lib/user_input.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_user_input(value) ⇒ Object



135
136
137
# File 'lib/user_input.rb', line 135

def Array.from_user_input(value)
  return [*value]
end

Instance Method Details

#from_user_input(value) ⇒ Object

Checks each element of the value array to ensure that they match against the first element of self



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/user_input.rb', line 141

def from_user_input(value)
  value = [*value]
  # eliminate the obvious
  if (self.length != 1)
    raise ArgumentError, "Must supply only one element to an array you're calling from_user_input on."
  end
  innertype = self[0]
  # now check whether the inner elements of the array match
  output = value.collect {|innervalue|
    # if innertype is not an array, but value is, we need to flatten it
    if (!innertype.kind_of?(Array) && innervalue.kind_of?(Array))
      innervalue = innervalue[0]
    end
    innertype.from_user_input(innervalue); # returns
  }.compact()

  if (output.length > 0)
    return output
  else
    return nil
  end
end