On Oct 17, 4:53 am, Shuaib Z. [email protected] wrote:
I want to declare 2-dimensional arrays it has 6 columns but uknow number
of columns.
i tried this way
array = [][] # did not work
array = [[],[]] # it works but only for two elements.
There is no such thing as a 2-dimensional array in Ruby (not in the
core, anyhow). Using core objects, you can instead create an array of
arrays (as you have done above).
Hereâs an example of some code that automatically creates rows with 6
nil values each time you ask for a row:
class SixColArray < Array
def initialize( num_rows=0 )
# Simply asking for a row ensures it and all lesser rows exist
self[num_rows-1] if num_rows > 0
end
def
unless row = super
# Create a new row with 6 âcolumnâ arrays
row = self[ row_number ] = Array.new(6)
# Ensure lower row numbers exist by asking for them
recursively
self[ row_number - 1 ] if row_number > 0
end
row
end
end
require âppâ
six_col = SixColArray.new( 4 )
six_col[ 0 ][ 3 ] = 42
six_col[ 3 ][ 1 ] = 15
pp six_col
#=> [[nil, nil, nil, 42, nil, nil],
#=> [nil, nil, nil, nil, nil, nil],
#=> [nil, nil, nil, nil, nil, nil],
#=> [nil, 15, nil, nil, nil, nil]]
A new row is created on the fly
six_col[ 4 ][ 5 ] = 99
pp six_col
#=> [[nil, nil, nil, 42, nil, nil],
#=> [nil, nil, nil, nil, nil, nil],
#=> [nil, nil, nil, nil, nil, nil],
#=> [nil, 15, nil, nil, nil, nil],
#=> [nil, nil, nil, nil, nil, 99]]
There is no limit on the number of columns in a row
âŚor the type of values for each slot
six_col[ 2 ][ 8 ] = âowâ
pp six_col
#=> [[nil, nil, nil, 42, nil, nil],
#=> [nil, nil, nil, nil, nil, nil],
#=> [nil, nil, nil, nil, nil, nil, nil, nil, âowâ],
#=> [nil, 15, nil, nil, nil, nil],
#=> [nil, nil, nil, nil, nil, 99]]
If you want something more robust (for example ensuring that you canât
create new columns on the fly), look for true matrix classes. (One
exists in the standard library; see
http://ruby-doc.org/stdlib/libdoc/matrix/rdoc/index.html
for more information. Also see the NArray class.)
another question I believe the answer is no but i want to confirm
can we have multi-dimensional hash?
Of course, they are called hashes of hashes. For example:
people = {
:gavin => {
:age => 34,
:sex => :male
},
:lisa => {
:age => 33,
:sex => female
}
}
p people[ :lisa ][ :age ]
#=> 33
Hereâs a Hash that automatically creates new hashes for each key you
try to access (specifically 1-level deep for a â2-dimensionalâ hash):
people = Hash.new{ |me,key| me[ key ] = {} }
people[ :gavin ][ :age ] = 34
people[ :gavin ][ :sex ] = :male
people[ :fido ][ :species ] = :dog
p people
#=> {:gavin=>{:age=>34, :sex=>:male}, :fido=>{:species=>:dog}}
As you can see, again, there is no constraint on what sort of keys are
allowed at any level.
I hope this helps you see how:
a) The core classes of Ruby do not include a lot of very specialized
cases,
b) You can write any classes you want (and someone probably already
has)
c) If you donât require the program to ensure you donât go âout of
boundsâ, you can use all sorts of nested objects to create what you
want.