ruby-tables 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -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 ]
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruby-tables}
8
- s.version = "0.1.4"
8
+ s.version = "0.1.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nicholas Wright"]
@@ -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 ]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-tables
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas Wright