ruby-tables 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +20 -0
- data/VERSION +1 -1
- data/lib/table.rb +37 -0
- data/ruby-tables.gemspec +1 -1
- data/spec/table_spec.rb +31 -0
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -56,3 +56,23 @@ in when you create a key well out of bounds. This is not unlike typical ruby Arr
|
|
56
56
|
t3.pairs #=> { :something => 50, :else => 100, :a => 1000, :b => 2000, :c => 50, :d => 60 }
|
57
57
|
t3.to_a #=> [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
|
58
58
|
t3.inject { |sum, i | sum + i } #=> 45
|
59
|
+
|
60
|
+
# other examples
|
61
|
+
t = Table[ 1, 2, 3, 4, { :a => "1", :b => "2" }, 7, 8 ]
|
62
|
+
t #=> Table[1, 2, 3, 4, 7, 8, {:b=>"2", :a=>"1"}]
|
63
|
+
|
64
|
+
t.size #=> 8
|
65
|
+
t.b #=> "2"
|
66
|
+
|
67
|
+
t.b = Table[ 255, 0, 0, { :color => "red" } ]
|
68
|
+
t.b.color #=> "red"
|
69
|
+
r, b, g = *t.b
|
70
|
+
print r, g, b #=> 25500
|
71
|
+
|
72
|
+
t #=> Table[1, 2, 3, 4, 7, 8, {:b=>Table[255, 0, 0, {:color=>"red"}], :a=>"1", :c=>"3"}]
|
73
|
+
|
74
|
+
t << %{ a b c d }
|
75
|
+
t.last #=> ["a", "b", "c", "d"]
|
76
|
+
t[ -3 ] #=> 7
|
77
|
+
|
78
|
+
t #=> Table[1, 2, 3, 4, 7, 8, ["a", "b", "c", "d"], {:b=>Table[255, 0, 0, {:color=>"red"}], :a=>"1", :c=>"3"}]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/lib/table.rb
CHANGED
@@ -111,6 +111,43 @@ class Table
|
|
111
111
|
process arg
|
112
112
|
end
|
113
113
|
|
114
|
+
alias push <<
|
115
|
+
|
116
|
+
# removes and returns the last non pair element
|
117
|
+
#
|
118
|
+
# t = Table[ 1, 2, 3 ]
|
119
|
+
#
|
120
|
+
# t.pop #=> 3
|
121
|
+
# t.size #=> 2
|
122
|
+
def pop
|
123
|
+
@values.pop
|
124
|
+
end
|
125
|
+
|
126
|
+
# removes and returns the first non pair element
|
127
|
+
#
|
128
|
+
# t = Table[ 1, 2, 3 ]
|
129
|
+
#
|
130
|
+
# t.shift #=> 1
|
131
|
+
# t.size #=> 2
|
132
|
+
def shift
|
133
|
+
@values.shift
|
134
|
+
end
|
135
|
+
|
136
|
+
# deletes a key or index from a table
|
137
|
+
#
|
138
|
+
# t = Table[ 1000, 2000, 3000, { :a => 50, :b => 60 } ]
|
139
|
+
#
|
140
|
+
# t.delete 1 #=> 2000
|
141
|
+
# t.delete :b #=> 60
|
142
|
+
# t #=> Table[1000, 3000, {:a=>50}]
|
143
|
+
def delete key
|
144
|
+
if key.kind_of? Integer
|
145
|
+
@values.delete_at key
|
146
|
+
else
|
147
|
+
@records.delete key
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
114
151
|
# combines 2 tables
|
115
152
|
#
|
116
153
|
# t = Table[ :a => 4, :b => 5 ] + Table[ 1, 2, 3, 4, { :c => 4 } ]
|
data/ruby-tables.gemspec
CHANGED
data/spec/table_spec.rb
CHANGED
@@ -91,6 +91,37 @@ describe Table do
|
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
|
+
describe "#pop" do
|
95
|
+
it "removes and returns the last non pair element" do
|
96
|
+
t = Table[ 1, 2, 3, 4 ]
|
97
|
+
t.pop.should == 4
|
98
|
+
t.size.should == 3
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "#shift" do
|
103
|
+
it "removes and returns the first non pair element" do
|
104
|
+
t = Table[ 1, 2, 3, 4 ]
|
105
|
+
t.shift.should == 1
|
106
|
+
t.size.should == 3
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "#delete" do
|
111
|
+
it "removes and returns an index" do
|
112
|
+
t = Table[ 1, 2, 3, 4 ]
|
113
|
+
t.delete( 1 ).should == 2
|
114
|
+
t.size.should == 3
|
115
|
+
t.to_a.should == [ 1, 3, 4 ]
|
116
|
+
end
|
117
|
+
|
118
|
+
it "removes and returns an key" do
|
119
|
+
t = Table[ :a => 1, :b => 5 ]
|
120
|
+
t.delete( :b ).should == 5
|
121
|
+
t.should_not have_key( :b )
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
94
125
|
describe "#+" do
|
95
126
|
before :all do
|
96
127
|
@t1 = Table[ 1, 2, 3, { :a => 5, :b => 6 } ]
|