Module: STL

Included in:
FunctioncallNode
Defined in:
lib/STL.rb

Overview

Module for built-in functions.

Class Method Summary collapse

Class Method Details

.delete_element(array, index) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/STL.rb', line 50

def self.delete_element(array, index)
  unless array.is_a?(Array)
    raise TypeError, "Function #{__method__} must be used on an array."
  end
  unless index.between?(0, array.size-1)
    raise IndexError, "Index out of bounds. Max index: #{array.size-1}."
  end

  element = array[index]
  array.delete_at(index)
  return element
end

.get_element(array, index) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/STL.rb', line 38

def self.get_element(array, index)
  unless array.is_a?(Array)
    raise TypeError, "Function #{__method__} must be used on an array."
  end
  unless index.between?(0, array.size-1)
    raise IndexError, "Index out of bounds. Max index: #{array.size-1}."
  end

  element = array[index]
  return element
end

.pop(array) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/STL.rb', line 18

def self.pop(array)
  unless array.is_a?(Array)
    raise TypeError, "Function #{__method__} must be used on an array."
  end
  if array.empty?
    raise IndexError, "Can not use pop on an empty array."
  end
  pop_element = array.last
  array.delete_at(-1)
  return pop_element
end


5
6
7
8
# File 'lib/STL.rb', line 5

def self.print(x)
  pp x
  return nil
end

.push(array, element) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/STL.rb', line 30

def self.push(array, element)
  unless array.is_a?(Array)
    raise TypeError, "Function #{__method__} must be used on an array."
  end
  array << element
  return array
end

.size(x) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/STL.rb', line 10

def self.size(x)
  unless x.is_a?(Array) || x.is_a?(String) 
    raise TypeError, "Function #{__method__} must be used on a string or array."
  end
  
  return x.length
end