Class: Array

Inherits:
Object show all
Defined in:
lib/ruby_code/Array.rb

Instance Method Summary collapse

Instance Method Details

#cauldron_method_callsObject



3
4
5
6
7
8
9
# File 'lib/ruby_code/Array.rb', line 3

def cauldron_method_calls
  # TODO  Need to follow sexp more for ArrayAccess - I shouldn't need a class to contain the array and value
  #result = self.length.times.collect {|x| '['+x.to_s+']'}
  #result = self.length.times.collect {|x| ArrayAccess.new(self.copy,x)}
  result = ['.length','.any?','[]']
  return result
end

#contains?(&block) ⇒ Boolean

This is a variation on the ‘any?’ except if the array contains an array it continue the search into that array.

TODO This method could probably be cleaned up a bit

Parameters:

  • &block

    The block that evaluates if something exists in the array

Returns:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_code/Array.rb', line 57

def contains?(&block)
  self.each do |x|
    if x.respond_to?(:contains?) 
      if x.contains?(&block)
        return true
      end
    end
    return true if block.call(x)
  end
  return false
end

#copyObject

Raises:

  • (StandardError)


39
40
41
42
43
44
# File 'lib/ruby_code/Array.rb', line 39

def copy
  raise StandardError.new('No "copy" method has been created for '+self.class.to_s) unless self.class.to_s == 'Array'    
  result = []
  self.each {|x| result.push(x.copy) }
  return result
end

#select_all(results = [], &block) ⇒ Object

Returns all the elements that match the selection criteria just like the select method. However if it contains an array it will seach the array as well.

Parameters:

  • results (defaults to: [])

    An array of current results that match the requirement of the block.

  • block

    .



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby_code/Array.rb', line 77

def select_all(results=[],&block)
  self.each do |x|
    if block.call(x)
      results.push(x)
    end
    if x.respond_to?(:select_all) 
      x.select_all(results,&block)  
    end
  end
  return results
end

#to_declarationObject

Returns the array as a declaration string e.g. [‘lions’,‘tigers’,‘bears’] becomes “[‘lion’,‘tigers’,‘bears’]”

Raises:

  • (StandardError)


23
24
25
26
# File 'lib/ruby_code/Array.rb', line 23

def to_declaration
  raise StandardError.new('No to_declaration method has been created for '+self.class.to_s) unless self.class.to_s == 'Array'
  return LiteralDeclaration.new(self.clone)
end

#to_intrinsicObject

Raises:

  • (StandardError)


89
90
91
92
93
# File 'lib/ruby_code/Array.rb', line 89

def to_intrinsic
  # TODO  SHould be all? not any?
  return IntrinsicTestCases.new if self.any? {|x| x.kind_of?(CTestCase)}  
  raise StandardError.new('Can not be converted to intrinsic instance '+self.class.to_s)
end

#to_literalObject



46
47
48
# File 'lib/ruby_code/Array.rb', line 46

def to_literal
  return Literal.new(self.copy.collect {|x| x.to_literal})  
end

#to_var(id = nil, unique_id = nil) ⇒ Object

Raises:

  • (StandardError)


11
12
13
14
15
16
17
18
# File 'lib/ruby_code/Array.rb', line 11

def to_var(id=nil,unique_id=nil)
  raise StandardError.new('No to_var method has been created for '+self.class.to_s) unless self.class.to_s == 'Array'
  # TODO I'm not sure whether it's best to save the literal value or variable value
  result = []
  self.each {|x| result.push(x.copy)}    
  replicate = ArrayVariable.new(*result) {{:variable_id => id,:uniq_id=>unique_id}}
  return replicate
end

#writeObject

Raises:

  • (StandardError)


28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_code/Array.rb', line 28

def write
  raise StandardError.new('No "write" method has been created for '+self.class.to_s) unless self.class.to_s == 'Array'
  line = '['    
  self.each do |x|
    line += x.write
    unless x.object_id == self.last.object_id then line += ', ' end
  end
  line += ']'
  return line    
end