Creating each method for custom two dimensional array
-
07-07-2021 - |
Question
I followed these instructions to create a custom class for two dimensional arrays.
class SparseArray
attr_reader :hash
def initialize
@hash = {}
end
def [](key)
hash[key] ||= {}
end
def rows
hash.length
end
alias_method :length, :rows
end
How can I modify this class so I can iterate through the first and second level of an object using Object#each do? Please explain in simple terms, I'm a newbie.
Example of how I would use the each method on an object:
sparse_array[0][0] = "row 1 column 1"
sparse_array[0][1] = "row 1 column 2"
sparse_array[1][0] = "row 2 column 1"
sparse_array.each do |row|
# sparse_array[0] on first iteration
row.each do |column|
# sparse_array[0][0] on first iteration
end
end
Solution
You should define each
method on your SparseArray
and use it to wrap values
of the wrapped hash:
def each &block
@hash.values.each(&block)
end
OTHER TIPS
Hash
has an each
method as well:
hash.each {|key, value| puts "#{key} #{value}" }
But if you want a nicer method, put this in your class:
def each(&block)
hash.each do |row,column_hash|
column_hash.each do |column, value|
block.call(row,column,value)
end
end
end
Then you can do things like this:
sparse_array.each do |row, column, value|
puts "row: #{row}, column: #{column}, value: #{value}"
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow