ruby-tables 0.1.4 → 0.1.5
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/VERSION +1 -1
- data/lib/table.rb +31 -0
- data/ruby-tables.gemspec +1 -1
- data/spec/table_spec.rb +24 -0
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
data/lib/table.rb
CHANGED
@@ -55,6 +55,11 @@ class Table
|
|
55
55
|
#
|
56
56
|
# t.a #=> 4
|
57
57
|
# t.b #=> 5
|
58
|
+
#
|
59
|
+
# array slicing can also be performed:
|
60
|
+
#
|
61
|
+
# t[ 1, 2 ] #=> [2, "string"]
|
62
|
+
# t[ 1..4 ] #=> [2, "string", 3, 4]
|
58
63
|
def [] key, *rest
|
59
64
|
return @values.slice( key, *rest ) if rest.any?
|
60
65
|
|
@@ -113,6 +118,32 @@ class Table
|
|
113
118
|
|
114
119
|
alias push <<
|
115
120
|
|
121
|
+
# insert an element into a table at a key or index
|
122
|
+
#
|
123
|
+
# if an index is used, multiple values can be added at that location:
|
124
|
+
#
|
125
|
+
# t = Table[ 1, 2, 3, 4 ]
|
126
|
+
# t.insert 2, 1000, 2000, 3000
|
127
|
+
# t #=> Table[1, 2, 1000, 2000, 3000, 3, 4]
|
128
|
+
#
|
129
|
+
# t.insert 4, 1000000
|
130
|
+
# t #=> Table[1, 2, 1000, 2000, 1000000, 3000, 3, 4]
|
131
|
+
#
|
132
|
+
# when a key is used, the functionality is identical to #[]=:
|
133
|
+
#
|
134
|
+
# t.insert :a, "apple"
|
135
|
+
# t.insert :b, "banana"
|
136
|
+
# t.a #=> "apple"
|
137
|
+
# t.b #=> "banana"
|
138
|
+
# t => Table[1, 2, 1000, 2000, 1000000, 3000, 3, 4, {:b=>"banana", :a=>"apple"}]
|
139
|
+
def insert key, *args
|
140
|
+
if key.kind_of? Integer
|
141
|
+
@values.insert key, *args
|
142
|
+
else
|
143
|
+
process_hash key => args.first
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
116
147
|
# removes and returns the last non pair element
|
117
148
|
#
|
118
149
|
# t = Table[ 1, 2, 3 ]
|
data/ruby-tables.gemspec
CHANGED
data/spec/table_spec.rb
CHANGED
@@ -91,6 +91,30 @@ describe Table do
|
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
|
+
describe "#insert" do
|
95
|
+
it "should insert a value into the table at index" do
|
96
|
+
t = Table[ 1, 2, 3, 4 ]
|
97
|
+
t.insert 2, 5
|
98
|
+
t[ 2 ].should == 5
|
99
|
+
t.size.should == 5
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should be able to insert multiple values into the table at index" do
|
103
|
+
t = Table[ 1, 2, 3, 4 ]
|
104
|
+
old_size = t.size
|
105
|
+
vals_to_add = ( 1..10 ).map
|
106
|
+
t.insert 2, *vals_to_add
|
107
|
+
t[ 2, 10 ].should == vals_to_add
|
108
|
+
t.size.should == old_size + vals_to_add.size
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should insert a value into the table at key" do
|
112
|
+
t = Table[ :a => 1, :b => 2 ]
|
113
|
+
t.insert :c, 1000
|
114
|
+
t.c.should == 1000
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
94
118
|
describe "#pop" do
|
95
119
|
it "removes and returns the last non pair element" do
|
96
120
|
t = Table[ 1, 2, 3, 4 ]
|