ruby-tables 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,9 @@ Any methods from the Enumerable module should work and will treat the table as a
10
10
  ignoring any pairs inside it.
11
11
 
12
12
  = Caveats
13
- As in lua, hash keys cannot be numeric indices. If you intend to use numeric hash keys,
13
+ Like a hash, the order for paired objects is not guaranteed.
14
+
15
+ Also, as in lua, hash keys cannot be numeric indices. If you intend to use numeric hash keys,
14
16
  they should be inside an array, such as:
15
17
  Table[ [ 2 ] => 4 ]
16
18
 
data/Rakefile CHANGED
@@ -4,10 +4,10 @@ require 'rake'
4
4
  begin
5
5
  require 'jeweler'
6
6
  Jeweler::Tasks.new do |gem|
7
- gem.version = "0.1.0"
7
+ gem.version = "0.1.1"
8
8
  gem.name = "ruby-tables"
9
- gem.summary = %Q{Ruby implementation of lua tables}
10
- gem.description = %Q{A table data structure implemented in ruby}
9
+ gem.summary = %Q{Lua tables for Ruby}
10
+ gem.description = %Q{A table data structure implemented in Ruby}
11
11
  gem.email = "nick.loves.rails@gmail.com"
12
12
  gem.homepage = "http://github.com/Abica/ruby-tables"
13
13
  gem.authors = [ "Nicholas Wright" ]
@@ -1,11 +1,40 @@
1
- # Implements a table data structure
1
+ #
2
+ # A table is a basically a combination of an array and a hash.
3
+ #
4
+ # Actually a possibly easier way to think of them would be as
5
+ # Arrays that have some metadata in the form of a hash.
6
+ #
7
+ # You can also access symbolized hash keys with dot notation, making them
8
+ # really convenient for a lot of things.
9
+ #
10
+ # For instance:
11
+ #
12
+ # t = Table[ 1, 2, 3, 4, { :a => "1", :b => "2" }, 7, 8 ]
13
+ # t #=> Table[1, 2, 3, 4, 7, 8, {:b=>"2", :a=>"1"}]
14
+ #
15
+ # t.size #=> 8
16
+ # t.b #=> "2"
17
+ #
18
+ # t.b = Table[ 255, 0, 0, { :color => "red" } ]
19
+ # t.b.color #=> "red"
20
+ # r, b, g = *t.b
21
+ # print r, g, b #=> 25500
22
+ #
23
+ # t #=> Table[1, 2, 3, 4, 7, 8, {:b=>Table[255, 0, 0, {:color=>"red"}], :a=>"1", :c=>"3"}]
24
+ #
25
+ # t << %{ a b c d }
26
+ # t.last #=> ["a", "b", "c", "d"]
27
+ # t[ -3 ] #=> 7
28
+ #
29
+ # t #=> Table[1, 2, 3, 4, 7, 8, ["a", "b", "c", "d"], {:b=>Table[255, 0, 0, {:color=>"red"}], :a=>"1", :c=>"3"}]
30
+ #
2
31
  class Table
3
32
  include Enumerable
4
33
 
5
34
  # takes a comma separated list of arrays and hashes and returns a table
6
35
  #
7
- # example:
8
- # Table[ 1, 2, 3, 4, { :a => 3, :b => 5 }, 7, 8, { :c => 33 } ]
36
+ # t = Table[ 1, 2, 3, 4, { :a => 3, :b => 5 }, 7, 8, { :c => 33 } ]
37
+ # t #=> Table[1, 2, 3, 4, 7, 8, {:a=>3, :b=>5, :c=>33}]
9
38
  def self.[] *args
10
39
  new *args
11
40
  end
@@ -26,11 +55,16 @@ class Table
26
55
  #
27
56
  # t.a #=> 4
28
57
  # t.b #=> 5
29
- def [] key
30
- if key.is_a? Integer
31
- @values[ key ]
32
- else
33
- @records[ key ]
58
+ def [] key, *rest
59
+ return @values.slice( key, *rest ) if rest.any?
60
+
61
+ case key
62
+ when Range
63
+ @values.slice key
64
+ when Integer
65
+ @values[ key ]
66
+ else
67
+ @records[ key ]
34
68
  end
35
69
  end
36
70
 
@@ -38,8 +72,10 @@ class Table
38
72
  # if no entry exists for the given key or index then one is created
39
73
  #
40
74
  # t = Table[ :a => "abcde", :b => 44332211 ]
75
+ #
41
76
  # t[ :a ] = 43
42
77
  # t[ :a ] #=> 43
78
+ #
43
79
  # t[ 0 ] = 54
44
80
  # t.first #=> 54
45
81
  #
@@ -62,17 +98,25 @@ class Table
62
98
  end
63
99
  end
64
100
 
65
- # add a hash or value to this table
101
+ # adds a hash or value to a table
66
102
  #
67
103
  # t = Table[ 1, 2, 3 ]
104
+ #
68
105
  # t << { :a => 4, :b => 4 }
106
+ # t.pairs #=> { :a => 4, :b => 4 }
107
+ #
108
+ # t << 40000
109
+ # t.to_a #=> [ 1, 2, 3, 40000 ]
69
110
  def << arg
70
111
  process arg
71
112
  end
72
113
 
73
114
  # combines 2 tables
74
115
  #
75
- # Table[ :a => 4, :b => 5 ] + Table[ 1, 2, 3, 4, { :c => 4 } ]
116
+ # t = Table[ :a => 4, :b => 5 ] + Table[ 1, 2, 3, 4, { :c => 4 } ]
117
+ #
118
+ # t.pairs #=> { :a => 4, :b => 5, :c => 4 }
119
+ # t.to_a #=> [ 1, 2, 3, 4 ]
76
120
  def + other
77
121
  values = self.to_a + other.to_a
78
122
  Table[ self.pairs, other.pairs, *values ]
@@ -117,12 +161,30 @@ class Table
117
161
  @values.sort &block
118
162
  end
119
163
 
164
+ # slice a table like an array
165
+ #
166
+ # t = Table[ 2, 23, 54, { :a => 4 }, 49 ]
167
+ # t[ 2..4 ] #=> [ 54, 49 ]
168
+ def slice *args
169
+ @values.slice *args
170
+ end
171
+
120
172
  # iterate through the key => value pairs in the table
173
+ #
174
+ # t = Table[ 1, 2, { :a => "cat", :b => "dog" } ]
175
+ #
176
+ # t.each_pair { | k, v | print k, v }
177
+ # #=> bdogacat
121
178
  def each_pair &block
122
179
  @records.each_pair &block
123
180
  end
124
181
 
125
182
  # iterate through the hash keys in the table
183
+ #
184
+ # t = Table[ 1, 2, { :a => "cat", :b => "dog" } ]
185
+ #
186
+ # t.each_key { | k | print k }
187
+ # #=> ab
126
188
  def each_key &block
127
189
  @records.each_key &block
128
190
  end
@@ -151,7 +213,18 @@ class Table
151
213
  @records.values
152
214
  end
153
215
 
216
+ def inspect
217
+ hsh = pairs
218
+ str = []
219
+
220
+ str << map { | item | item.inspect } if any?
221
+ str << "{#{ hsh.map { | key, val | "#{ key.inspect }=>#{ val.inspect }" } }}" if hsh.any?
222
+
223
+ "Table[#{ str.join( ", " ) }]"
224
+ end
225
+
154
226
  private
227
+ # adds +args+ into the table
155
228
  def process *args
156
229
  args.each do | arg |
157
230
  if arg.is_a? Hash
@@ -162,23 +235,27 @@ class Table
162
235
  end
163
236
  end
164
237
 
238
+ # adds a hash entries to the table and creates
239
+ # setters and getters for them if the keys are symbolized
240
+ #
241
+ # any keys that are numeric are wrapped in an array so as
242
+ # not to overlap array indices which could get confusing
165
243
  def process_hash hsh
166
244
  hsh.each do | key, value |
167
245
  key = [ key ] if key.is_a? Integer
168
246
  @records[ key ] = value
169
247
 
170
- next unless key.is_a? Symbol
171
- next if respond_to? key and respond_to? "#{ key }="
248
+ if key.is_a? Symbol and not respond_to? "#{ key }="
249
+ instance_eval <<-EOM
250
+ def #{ key }
251
+ @records[ :#{ key } ]
252
+ end
172
253
 
173
- instance_eval <<-EOM
174
- def #{ key }
175
- @records[ :#{ key } ]
176
- end
177
-
178
- def #{ key }= value
179
- @records[ :#{ key } ] = value
180
- end
181
- EOM
254
+ def #{ key }= value
255
+ @records[ :#{ key } ] = value
256
+ end
257
+ EOM
258
+ end
182
259
  end
183
260
  end
184
261
 
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruby-tables}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
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"]
12
- s.date = %q{2010-02-13}
13
- s.description = %q{A table data structure implemented in ruby}
12
+ s.date = %q{2010-02-14}
13
+ s.description = %q{A table data structure implemented in Ruby}
14
14
  s.email = %q{nick.loves.rails@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "README.rdoc"
@@ -29,7 +29,7 @@ Gem::Specification.new do |s|
29
29
  s.rdoc_options = ["--charset=UTF-8"]
30
30
  s.require_paths = ["lib"]
31
31
  s.rubygems_version = %q{1.3.5}
32
- s.summary = %q{Ruby implementation of lua tables}
32
+ s.summary = %q{Lua tables for Ruby}
33
33
  s.test_files = [
34
34
  "spec/spec_helper.rb",
35
35
  "spec/table_spec.rb"
@@ -26,6 +26,14 @@ describe Table do
26
26
  @table[ :k ].should == @args[ 5 ][ :k ]
27
27
  @table[ :v ].should == @args[ 8 ][ :v ]
28
28
  end
29
+
30
+ it "returns an array when sliced with a range" do
31
+ @table[ 1..3 ].should == @array_vals[ 1..3 ]
32
+ end
33
+
34
+ it "returns an array when sliced with a start and length" do
35
+ @table[ 4, 5 ].should == @array_vals[ 4, 5 ]
36
+ end
29
37
  end
30
38
 
31
39
  describe "#[]=" do
@@ -143,6 +151,16 @@ describe Table do
143
151
  end
144
152
  end
145
153
 
154
+ describe "#slice" do
155
+ it "returns a sub array based on a range" do
156
+ @table.slice( 1..3 ).should == @array_vals.slice( 1..3 )
157
+ end
158
+
159
+ it "returns a sub array based on a start and length" do
160
+ @table.slice( 4, 5 ).should == @array_vals.slice( 4, 5 )
161
+ end
162
+ end
163
+
146
164
  describe "#pairs" do
147
165
  it "returns a hash of all of the key => value pairs" do
148
166
  pairs = @table.pairs
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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas Wright
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-13 00:00:00 -07:00
12
+ date: 2010-02-14 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,7 +22,7 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: 1.2.2
24
24
  version:
25
- description: A table data structure implemented in ruby
25
+ description: A table data structure implemented in Ruby
26
26
  email: nick.loves.rails@gmail.com
27
27
  executables: []
28
28
 
@@ -66,7 +66,7 @@ rubyforge_project:
66
66
  rubygems_version: 1.3.5
67
67
  signing_key:
68
68
  specification_version: 3
69
- summary: Ruby implementation of lua tables
69
+ summary: Lua tables for Ruby
70
70
  test_files:
71
71
  - spec/spec_helper.rb
72
72
  - spec/table_spec.rb