Class: Table
Overview
A table is a basically a combination of an array and a hash.
Actually a possibly easier way to think of them would be as Arrays that have some metadata in the form of a hash.
You can also access symbolized hash keys with dot notation, making them really convenient for a lot of things.
For instance:
t = Table[ 1, 2, 3, 4, { :a => "1", :b => "2" }, 7, 8 ]
t #=> Table[1, 2, 3, 4, 7, 8, {:b=>"2", :a=>"1"}]
t.size #=> 8
t.b #=> "2"
t.b = Table[ 255, 0, 0, { :color => "red" } ]
t.b.color #=> "red"
r, b, g = *t.b
print r, g, b #=> 25500
t #=> Table[1, 2, 3, 4, 7, 8, {:b=>Table[255, 0, 0, {:color=>"red"}], :a=>"1", :c=>"3"}]
t << %{ a b c d }
t.last #=> ["a", "b", "c", "d"]
t[ -3 ] #=> 7
t #=> Table[1, 2, 3, 4, 7, 8, ["a", "b", "c", "d"], {:b=>Table[255, 0, 0, {:color=>"red"}], :a=>"1", :c=>"3"}]
Class Method Summary collapse
-
.[](*args) ⇒ Object
takes a comma separated list of arrays and hashes and returns a table.
Instance Method Summary collapse
-
#+(other) ⇒ Object
combines 2 tables.
-
#<<(arg) ⇒ Object
adds a hash or value to a table.
-
#[](key, *rest) ⇒ Object
returns the value corresponding to the index or key.
-
#[]=(key, value) ⇒ Object
updates the value for a given key or index if no entry exists for the given key or index then one is created.
-
#each(&block) ⇒ Object
iterate through each of the array values.
-
#each_key(&block) ⇒ Object
iterate through the hash keys in the table .
-
#each_pair(&block) ⇒ Object
iterate through the key => value pairs in the table.
-
#first ⇒ Object
returns the first non pair item in the table.
-
#initialize(*args) ⇒ Table
constructor
A new instance of Table.
- #inspect ⇒ Object
-
#keys ⇒ Object
return an array of all hash keys in the table.
-
#last ⇒ Object
returns the last non pair item in the table.
-
#pairs ⇒ Object
returns a hash of all key => value pairs inside the table.
-
#size ⇒ Object
(also: #length)
returns the length of all integer indexes.
-
#slice(*args) ⇒ Object
slice a table like an array.
-
#sort(&block) ⇒ Object
sort the array values.
-
#values ⇒ Object
returns an array of all hash values in the table.
Constructor Details
#initialize(*args) ⇒ Table
Returns a new instance of Table.
42 43 44 45 46 |
# File 'lib/table.rb', line 42 def initialize *args @values = [] @records = {} process *args end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth_id, *args) ⇒ Object (private)
if an accessor is called that doesn’t exist, the key => value are added into the table
264 265 266 267 268 269 |
# File 'lib/table.rb', line 264 def method_missing meth_id, *args name = meth_id.id2name if key = name[ /(\w+)=/, 1 ] process_hash key.to_sym => args.first end end |
Class Method Details
.[](*args) ⇒ Object
takes a comma separated list of arrays and hashes and returns a table
t = Table[ 1, 2, 3, 4, { :a => 3, :b => 5 }, 7, 8, { :c => 33 } ]
t #=> Table[1, 2, 3, 4, 7, 8, {:a=>3, :b=>5, :c=>33}]
38 39 40 |
# File 'lib/table.rb', line 38 def self.[] *args new *args end |
Instance Method Details
#+(other) ⇒ Object
120 121 122 123 |
# File 'lib/table.rb', line 120 def + other values = self.to_a + other.to_a Table[ self.pairs, other.pairs, *values ] end |
#<<(arg) ⇒ Object
adds a hash or value to a table
t = Table[ 1, 2, 3 ]
t << { :a => 4, :b => 4 }
t.pairs #=> { :a => 4, :b => 4 }
t << 40000
t.to_a #=> [ 1, 2, 3, 40000 ]
110 111 112 |
# File 'lib/table.rb', line 110 def << arg process arg end |
#[](key, *rest) ⇒ Object
returns the value corresponding to the index or key
t = Table[ 1, 2, "string", { :a => 4, :b => 5 }, 3, 4 ]
t[ 2 ] #=> "string"
t[ :a ] #=> 4
symbolized keys are also accessible via dot notation:
t.a #=> 4
t.b #=> 5
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/table.rb', line 58 def [] key, *rest return @values.slice( key, *rest ) if rest.any? case key when Range @values.slice key when Integer @values[ key ] else @records[ key ] end end |
#[]=(key, value) ⇒ Object
updates the value for a given key or index if no entry exists for the given key or index then one is created
t = Table[ :a => "abcde", :b => 44332211 ]
t[ :a ] = 43
t[ :a ] #=> 43
t[ 0 ] = 54
t.first #=> 54
note that like an array, if you insert a value into a table at an index that doesn’t yet exist, any gaps will be filled in with nil such as:
t[ 5 ] = 100
t.to_a #=> [ 54, nil, nil, nil, 100 ]
symbolized keys also have setters so that you can use dot notation:
t.a = "bbcec"
t.a #=> "bbcec"
93 94 95 96 97 98 99 |
# File 'lib/table.rb', line 93 def []= key, value if key.is_a? Integer @values[ key ] = value else process_hash key => value end end |
#each(&block) ⇒ Object
iterate through each of the array values
152 153 154 |
# File 'lib/table.rb', line 152 def each &block @values.each &block end |
#each_key(&block) ⇒ Object
iterate through the hash keys in the table
t = Table[ 1, 2, { :a => "cat", :b => "dog" } ]
t.each_key { | k | print k }
#=> ab
188 189 190 |
# File 'lib/table.rb', line 188 def each_key &block @records.each_key &block end |
#each_pair(&block) ⇒ Object
iterate through the key => value pairs in the table
t = Table[ 1, 2, { :a => "cat", :b => "dog" } ]
t.each_pair { | k, v | print k, v }
#=> bdogacat
178 179 180 |
# File 'lib/table.rb', line 178 def each_pair &block @records.each_pair &block end |
#first ⇒ Object
returns the first non pair item in the table
t = Table[ { :a => 4 }, 1, 2, 3, 4, { :b => 4, :c => 23 } ]
t.first #=> 1
129 130 131 |
# File 'lib/table.rb', line 129 def first @values.first end |
#inspect ⇒ Object
216 217 218 219 220 221 222 223 224 |
# File 'lib/table.rb', line 216 def inspect hsh = pairs str = [] str << map { | item | item.inspect } if any? str << "{#{ hsh.map { | key, val | "#{ key.inspect }=>#{ val.inspect }" } }}" if hsh.any? "Table[#{ str.join( ", " ) }]" end |
#keys ⇒ Object
return an array of all hash keys in the table
t = Table[ 1, 2, { :a => 55, :b => 77 }, 3, { :c => 22 } ]
t.keys #=> [ :a, :b, :c ]
204 205 206 |
# File 'lib/table.rb', line 204 def keys @records.keys end |
#last ⇒ Object
returns the last non pair item in the table
t = Table[ { :a => 4 }, 1, 2, 3, 4, { :b => 4, :c => 23 } ]
t.last #=> 4
137 138 139 |
# File 'lib/table.rb', line 137 def last @values.last end |
#pairs ⇒ Object
returns a hash of all key => value pairs inside the table
t = Table[ 1, { :a => 4 }, 2, { :b => 543 } ]
t.pairs #=> { :a => 4, :b => 543 }
196 197 198 |
# File 'lib/table.rb', line 196 def pairs @records end |
#size ⇒ Object Also known as: length
returns the length of all integer indexes
t = Table[ 1, 2, { :b => 4 }, 3, 4 ]
t.size #=> 4
145 146 147 |
# File 'lib/table.rb', line 145 def size @values.size end |
#slice(*args) ⇒ Object
slice a table like an array
t = Table[ 2, 23, 54, { :a => 4 }, 49 ]
t[ 2..4 ] #=> [ 54, 49 ]
168 169 170 |
# File 'lib/table.rb', line 168 def slice *args @values.slice *args end |
#sort(&block) ⇒ Object
sort the array values
t = Table[ 2, 23, 54, { :a => 4 }, 49 ]
t.sort #=> [ 2, 23, 49, 54 ]
160 161 162 |
# File 'lib/table.rb', line 160 def sort &block @values.sort &block end |
#values ⇒ Object
returns an array of all hash values in the table
t = Table[ 1, 2, { :a => 55, :b => 77 }, 3, { :c => 22 } ]
t.values #=> [ 55, 77, 22 ]
212 213 214 |
# File 'lib/table.rb', line 212 def values @records.values end |