ruby-tables 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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.2
1
+ 0.1.3
@@ -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 } ]
@@ -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.2"
8
+ s.version = "0.1.3"
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,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 } ]
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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas Wright