Class: Tuple

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*elements) ⇒ Tuple

Returns a new instance of Tuple.



70
71
72
73
74
75
# File 'lib/static_ruby.rb', line 70

def initialize(*elements)
  @elements = elements
  @size = elements.size
  @first = elements.size != 0 ? elements[0] : nil
  @last = elements.size != 0 ? elements[-1] : nil
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



68
69
70
# File 'lib/static_ruby.rb', line 68

def elements
  @elements
end

#firstObject (readonly)

Returns the value of attribute first.



68
69
70
# File 'lib/static_ruby.rb', line 68

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



68
69
70
# File 'lib/static_ruby.rb', line 68

def last
  @last
end

#sizeObject (readonly)

Returns the value of attribute size.



68
69
70
# File 'lib/static_ruby.rb', line 68

def size
  @size
end

Instance Method Details

#!=(o) ⇒ Object



89
90
91
# File 'lib/static_ruby.rb', line 89

def !=(o)
  @elements != o.elements
end

#==(o) ⇒ Object



85
86
87
# File 'lib/static_ruby.rb', line 85

def ==(o)
  @elements == o.elements
end

#[](i) ⇒ Object



93
94
95
# File 'lib/static_ruby.rb', line 93

def [](i)
  @elements[i]
end

#[]=(_, _) ⇒ Object



97
98
99
# File 'lib/static_ruby.rb', line 97

def []=(_, _)
  raise('Tuple cannot be modified')
end

#each(&block) ⇒ Object



101
102
103
104
105
# File 'lib/static_ruby.rb', line 101

def each(&block)
  @elements.each do |x|
    block.call(x)
  end
end

#each_with_index(&block) ⇒ Object



107
108
109
110
111
# File 'lib/static_ruby.rb', line 107

def each_with_index(&block)
  @elements.each_with_index do |x, i|
    block.call(x, i)
  end
end

#to_aObject



77
78
79
# File 'lib/static_ruby.rb', line 77

def to_a
  @elements
end

#to_sObject



81
82
83
# File 'lib/static_ruby.rb', line 81

def to_s
  "Tuple(#{@elements.map { |x| x.to_s }})"
end