Class: Rubinius::Tuple

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rubinius/kernel/common/tuple.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](*args) ⇒ Object



13
14
15
16
17
# File 'lib/rubinius/kernel/common/tuple.rb', line 13

def self.[](*args)
  start = args.start
  tot = args.size
  return new(tot).copy_from(args.tuple, start, tot, 0)
end

._load(str) ⇒ Object



141
142
143
144
145
146
# File 'lib/rubinius/kernel/common/tuple.rb', line 141

def self._load(str)
  ary = Marshal.load(str)
  t = new(ary.size)
  ary.each_with_index { |obj, idx| t[idx] = obj }
  return t
end

Instance Method Details

#+(o) ⇒ Object



49
50
51
52
53
54
# File 'lib/rubinius/kernel/common/tuple.rb', line 49

def +(o)
  t = Tuple.new(size + o.size)
  t.copy_from(self,0,size,0)
  t.copy_from(o,0,o.size,size)
  t
end

#==(tup) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubinius/kernel/common/tuple.rb', line 33

def ==(tup)
  return super unless tup.kind_of?(Tuple)

  t = fields()

  return false unless t == tup.size

  i = 0
  while i < t
    return false unless at(i) == tup.at(i)
    i += 1
  end

  return true
end

#===(other) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/rubinius/kernel/common/tuple.rb', line 87

def ===(other)
  return false unless Tuple === other and fields == other.fields
  i = 0
  while i < fields
    return false unless at(i) === other.at(i)
    i += 1
  end
  true
end

#_dump(depth) ⇒ Object

Marshal support - _dump / _load are deprecated so eventually we should figure out a better way.



136
137
138
139
# File 'lib/rubinius/kernel/common/tuple.rb', line 136

def _dump(depth)
  # TODO use depth
  Marshal.dump to_a
end

#eachObject



23
24
25
26
27
28
29
30
31
# File 'lib/rubinius/kernel/common/tuple.rb', line 23

def each
  i = 0
  t = fields
  while i < t
    yield at(i)
    i += 1
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/rubinius/kernel/common/tuple.rb', line 122

def empty?
  size == 0
end

#firstObject



126
127
128
# File 'lib/rubinius/kernel/common/tuple.rb', line 126

def first
  at(0)
end

#inspectObject



56
57
58
59
60
61
62
63
64
# File 'lib/rubinius/kernel/common/tuple.rb', line 56

def inspect
  str = "#<#{self.class}"
  if fields == 0
    str << " empty>"
  else
    str << ": #{join(", ", :inspect)}>"
  end
  return str
end

#join(sep, meth = :to_s) ⇒ Object



66
67
68
# File 'lib/rubinius/kernel/common/tuple.rb', line 66

def join(sep, meth=:to_s)
  join_upto(sep, fields, meth)
end

#join_upto(sep, count, meth = :to_s) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rubinius/kernel/common/tuple.rb', line 70

def join_upto(sep, count, meth=:to_s)
  str = ""
  return str if count == 0 or empty?

  count = fields if count >= fields
  count -= 1
  i = 0
  while i < count
    str.append at(i).__send__(meth)
    str.append sep.dup
    i += 1
  end

  str.append at(count).__send__(meth)
  return str
end

#lastObject



130
131
132
# File 'lib/rubinius/kernel/common/tuple.rb', line 130

def last
  at(fields - 1)
end

#shiftObject



105
106
107
108
109
110
# File 'lib/rubinius/kernel/common/tuple.rb', line 105

def shift
  return self unless fields > 0
  t = Tuple.new(fields-1)
  t.copy_from self, 1, fields-1, 0
  return t
end

#swap(a, b) ⇒ Object

Swap elements of the two indexes.



113
114
115
116
117
# File 'lib/rubinius/kernel/common/tuple.rb', line 113

def swap(a, b)
  temp = at(a)
  put a, at(b)
  put b, temp
end

#to_aObject



97
98
99
100
101
102
103
# File 'lib/rubinius/kernel/common/tuple.rb', line 97

def to_a
  ary = []
  ary.tuple = dup
  ary.total = fields
  ary.start = 0
  return ary
end

#to_sObject



19
20
21
# File 'lib/rubinius/kernel/common/tuple.rb', line 19

def to_s
  "#<#{self.class}:0x#{object_id.to_s(16)} #{fields} elements>"
end